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

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mixpanel.android.R;
import com.mixpanel.android.mpmetrics.InAppNotification;
import com.mixpanel.android.mpmetrics.MPConfig;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import com.mixpanel.android.mpmetrics.Survey;
import com.mixpanel.android.mpmetrics.Survey.Question;
import com.mixpanel.android.mpmetrics.UpdateDisplayState;

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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Activity used internally by Mixpanel to display surveys and inapp takeover notifications.
 *
 * You should not send Intent's directly to display this activity. Instead use
 * {@link com.mixpanel.android.mpmetrics.MixpanelAPI.People#showSurveyIfAvailable(Activity)} and
 * {@link com.mixpanel.android.mpmetrics.MixpanelAPI.People#showNotificationIfAvailable(Activity)}
 */
@TargetApi(MPConfig.UI_FEATURES_MIN_API)
@SuppressLint("ClickableViewAccessibility")
public class SurveyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mIntentId = getIntent().getIntExtra(INTENT_ID_KEY, Integer.MAX_VALUE);
        mUpdateDisplayState = UpdateDisplayState.claimDisplayState(mIntentId);
        if (null == mUpdateDisplayState) {
            Log.e(LOGTAG, "SurveyActivity intent received, but nothing was found to show.");
            finish();
            return;
        }
        mMixpanel = MixpanelAPI.getInstance(SurveyActivity.this, mUpdateDisplayState.getToken());

        if (isShowingInApp()) {
            onCreateInAppNotification(savedInstanceState);
        } else if (isShowingSurvey()) {
            onCreateSurvey(savedInstanceState);
        } else {
            finish();
        }
    }

    private void onCreateInAppNotification(Bundle savedInstanceState) {
        setContentView(R.layout.com_mixpanel_android_activity_notification_full);

        final ImageView backgroundImage = (ImageView) findViewById(R.id.com_mixpanel_android_notification_gradient);
        final FadingImageView inAppImageView = (FadingImageView) findViewById(R.id.com_mixpanel_android_notification_image);
        final TextView titleView = (TextView) findViewById(R.id.com_mixpanel_android_notification_title);
        final TextView subtextView = (TextView) findViewById(R.id.com_mixpanel_android_notification_subtext);
        final Button ctaButton = (Button) findViewById(R.id.com_mixpanel_android_notification_button);
        final LinearLayout closeButtonWrapper = (LinearLayout) findViewById(R.id.com_mixpanel_android_button_exit_wrapper);

        final UpdateDisplayState.DisplayState.InAppNotificationState notificationState =
                (UpdateDisplayState.DisplayState.InAppNotificationState) mUpdateDisplayState.getDisplayState();
        final InAppNotification inApp = notificationState.getInAppNotification();

        // Layout
        final Display display = getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) closeButtonWrapper.getLayoutParams();
            params.setMargins(0, 0, 0, (int) (size.y * 0.06f)); // make bottom margin 6% of screen height
            closeButtonWrapper.setLayoutParams(params);
        }

        final GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.LEFT_RIGHT, // Ignored in radial gradients
            new int[]{ 0xE560607C, 0xE548485D, 0xE518181F, 0xE518181F }
        );
        gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            gd.setGradientCenter(0.25f, 0.5f);
            gd.setGradientRadius(Math.min(size.x, size.y) * 0.8f);
        } else {
            gd.setGradientCenter(0.5f, 0.33f);
            gd.setGradientRadius(Math.min(size.x, size.y) * 0.7f);
        }

        setViewBackground(backgroundImage, gd);

        titleView.setText(inApp.getTitle());
        subtextView.setText(inApp.getBody());

        final Bitmap inAppImage = inApp.getImage();
        inAppImageView.setBackgroundResource(R.drawable.com_mixpanel_android_square_dropshadow);

        if (inAppImage.getWidth() < SHADOW_SIZE_THRESHOLD_PX || inAppImage.getHeight() < SHADOW_SIZE_THRESHOLD_PX) {
            inAppImageView.setBackgroundResource(R.drawable.com_mixpanel_android_square_nodropshadow);
        } else {
            int h = inAppImage.getHeight() / 100;
            int w = inAppImage.getWidth() / 100;
            final Bitmap scaledImage = Bitmap.createScaledBitmap(inAppImage, w, h, false);
            int averageColor;
            int averageAlpha;
            outerloop:
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    averageColor = scaledImage.getPixel(x, y);
                    averageAlpha = Color.alpha(averageColor);
                    if (averageAlpha < 0xFF) {
                        inAppImageView.setBackgroundResource(R.drawable.com_mixpanel_android_square_nodropshadow);
                        break outerloop;
                    }
                }
            }
        }
        inAppImageView.setImageBitmap(inAppImage);

        final String ctaUrl = inApp.getCallToActionUrl();
        if (ctaUrl != null && ctaUrl.length() > 0) {
            ctaButton.setText(inApp.getCallToAction());
        }

        // Listeners
        ctaButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String uriString = inApp.getCallToActionUrl();
                if (uriString != null && uriString.length() > 0) {
                    Uri uri;
                    try {
                        uri = Uri.parse(uriString);
                    } catch (final IllegalArgumentException e) {
                        Log.i(LOGTAG, "Can't parse notification URI, will not take any action", e);
                        return;
                    }

                    try {
                        final Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
                        SurveyActivity.this.startActivity(viewIntent);
                        mMixpanel.getPeople().trackNotification("$campaign_open", inApp);
                    } catch (final ActivityNotFoundException e) {
                        Log.i(LOGTAG, "User doesn't have an activity for notification URI");
                    }
                }
                finish();
                UpdateDisplayState.releaseDisplayState(mIntentId);
            }
        });
        ctaButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
            public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setBackgroundResource(R.drawable.com_mixpanel_android_cta_button_highlight);<commit_msg>fixing whitespace<commit_after>package com.mixpanel.android.surveys;

import android.annotation.SuppressLint;
import android.annotation.TargetApi;
import android.app.Activity;
import android.app.AlertDialog;
import android.content.ActivityNotFoundException;
import android.content.DialogInterface;
import android.content.Intent;
import android.content.pm.ActivityInfo;
import android.content.res.Configuration;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.GradientDrawable;
import android.net.Uri;
import android.os.Build;
import android.os.Bundle;
import android.util.Log;
import android.view.Display;
import android.view.MotionEvent;
import android.view.View;
import android.view.animation.Animation;
import android.view.animation.AnimationUtils;
import android.view.animation.DecelerateInterpolator;
import android.view.animation.ScaleAnimation;
import android.view.animation.TranslateAnimation;
import android.widget.Button;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.mixpanel.android.R;
import com.mixpanel.android.mpmetrics.InAppNotification;
import com.mixpanel.android.mpmetrics.MPConfig;
import com.mixpanel.android.mpmetrics.MixpanelAPI;
import com.mixpanel.android.mpmetrics.Survey;
import com.mixpanel.android.mpmetrics.Survey.Question;
import com.mixpanel.android.mpmetrics.UpdateDisplayState;

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

import java.text.DateFormat;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;
import java.util.TimeZone;

/**
 * Activity used internally by Mixpanel to display surveys and inapp takeover notifications.
 *
 * You should not send Intent's directly to display this activity. Instead use
 * {@link com.mixpanel.android.mpmetrics.MixpanelAPI.People#showSurveyIfAvailable(Activity)} and
 * {@link com.mixpanel.android.mpmetrics.MixpanelAPI.People#showNotificationIfAvailable(Activity)}
 */
@TargetApi(MPConfig.UI_FEATURES_MIN_API)
@SuppressLint("ClickableViewAccessibility")
public class SurveyActivity extends Activity {
    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);

        mIntentId = getIntent().getIntExtra(INTENT_ID_KEY, Integer.MAX_VALUE);
        mUpdateDisplayState = UpdateDisplayState.claimDisplayState(mIntentId);
        if (null == mUpdateDisplayState) {
            Log.e(LOGTAG, "SurveyActivity intent received, but nothing was found to show.");
            finish();
            return;
        }
        mMixpanel = MixpanelAPI.getInstance(SurveyActivity.this, mUpdateDisplayState.getToken());

        if (isShowingInApp()) {
            onCreateInAppNotification(savedInstanceState);
        } else if (isShowingSurvey()) {
            onCreateSurvey(savedInstanceState);
        } else {
            finish();
        }
    }

    private void onCreateInAppNotification(Bundle savedInstanceState) {
        setContentView(R.layout.com_mixpanel_android_activity_notification_full);

        final ImageView backgroundImage = (ImageView) findViewById(R.id.com_mixpanel_android_notification_gradient);
        final FadingImageView inAppImageView = (FadingImageView) findViewById(R.id.com_mixpanel_android_notification_image);
        final TextView titleView = (TextView) findViewById(R.id.com_mixpanel_android_notification_title);
        final TextView subtextView = (TextView) findViewById(R.id.com_mixpanel_android_notification_subtext);
        final Button ctaButton = (Button) findViewById(R.id.com_mixpanel_android_notification_button);
        final LinearLayout closeButtonWrapper = (LinearLayout) findViewById(R.id.com_mixpanel_android_button_exit_wrapper);

        final UpdateDisplayState.DisplayState.InAppNotificationState notificationState =
                (UpdateDisplayState.DisplayState.InAppNotificationState) mUpdateDisplayState.getDisplayState();
        final InAppNotification inApp = notificationState.getInAppNotification();

        // Layout
        final Display display = getWindowManager().getDefaultDisplay();
        final Point size = new Point();
        display.getSize(size);

        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_PORTRAIT) {
            final RelativeLayout.LayoutParams params = (RelativeLayout.LayoutParams) closeButtonWrapper.getLayoutParams();
            params.setMargins(0, 0, 0, (int) (size.y * 0.06f)); // make bottom margin 6% of screen height
            closeButtonWrapper.setLayoutParams(params);
        }

        final GradientDrawable gd = new GradientDrawable(
            GradientDrawable.Orientation.LEFT_RIGHT, // Ignored in radial gradients
            new int[]{ 0xE560607C, 0xE548485D, 0xE518181F, 0xE518181F }
        );
        gd.setGradientType(GradientDrawable.RADIAL_GRADIENT);
        if (getResources().getConfiguration().orientation == Configuration.ORIENTATION_LANDSCAPE) {
            gd.setGradientCenter(0.25f, 0.5f);
            gd.setGradientRadius(Math.min(size.x, size.y) * 0.8f);
        } else {
            gd.setGradientCenter(0.5f, 0.33f);
            gd.setGradientRadius(Math.min(size.x, size.y) * 0.7f);
        }

        setViewBackground(backgroundImage, gd);

        titleView.setText(inApp.getTitle());
        subtextView.setText(inApp.getBody());

        final Bitmap inAppImage = inApp.getImage();
        inAppImageView.setBackgroundResource(R.drawable.com_mixpanel_android_square_dropshadow);

        if (inAppImage.getWidth() < SHADOW_SIZE_THRESHOLD_PX || inAppImage.getHeight() < SHADOW_SIZE_THRESHOLD_PX) {
            inAppImageView.setBackgroundResource(R.drawable.com_mixpanel_android_square_nodropshadow);
        } else {
            int h = inAppImage.getHeight() / 100;
            int w = inAppImage.getWidth() / 100;
            final Bitmap scaledImage = Bitmap.createScaledBitmap(inAppImage, w, h, false);
            int averageColor;
            int averageAlpha;
            outerloop:
            for (int x = 0; x < w; x++) {
                for (int y = 0; y < h; y++) {
                    averageColor = scaledImage.getPixel(x, y);
                    averageAlpha = Color.alpha(averageColor);
                    if (averageAlpha < 0xFF) {
                        inAppImageView.setBackgroundResource(R.drawable.com_mixpanel_android_square_nodropshadow);
                        break outerloop;
                    }
                }
            }
        }
        inAppImageView.setImageBitmap(inAppImage);

        final String ctaUrl = inApp.getCallToActionUrl();
        if (ctaUrl != null && ctaUrl.length() > 0) {
            ctaButton.setText(inApp.getCallToAction());
        }

        // Listeners
        ctaButton.setOnClickListener(new View.OnClickListener() {
            @Override
            public void onClick(View v) {
                final String uriString = inApp.getCallToActionUrl();
                if (uriString != null && uriString.length() > 0) {
                    Uri uri;
                    try {
                        uri = Uri.parse(uriString);
                    } catch (final IllegalArgumentException e) {
                        Log.i(LOGTAG, "Can't parse notification URI, will not take any action", e);
                        return;
                    }

                    try {
                        final Intent viewIntent = new Intent(Intent.ACTION_VIEW, uri);
                        SurveyActivity.this.startActivity(viewIntent);
                        mMixpanel.getPeople().trackNotification("$campaign_open", inApp);
                    } catch (final ActivityNotFoundException e) {
                        Log.i(LOGTAG, "User doesn't have an activity for notification URI");
                    }
                }
                finish();
                UpdateDisplayState.releaseDisplayState(mIntentId);
            }
        });
        ctaButton.setOnTouchListener(new View.OnTouchListener() {
            @Override
			public boolean onTouch(View v, MotionEvent event) {
                if (event.getAction() == MotionEvent.ACTION_DOWN) {
                    v.setBackgroundResource(R.drawable.com_mixpanel_android_cta_button_highlight);
{'fraction_non_alphanumeric': 0.0905486180978872, 'fraction_numerical': 0.00710185239983429, 'mean_word_length': 2.912479740680713, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 7, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5324433', 'n_tokens_mistral': 4700, 'n_tokens_neox': 4391, 'n_words': 848}