package com.mixpanel.android.util;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.Color;
import android.view.View;

public class ActivityImageUtils {
    // May return null.
    public static Bitmap getScaledScreenshot(final Activity activity, int scaleWidth, int scaleHeight, boolean relativeScaleIfTrue) {
        final View someView = activity.findViewById(android.R.id.content);
        final View rootView = someView.getRootView();
        final boolean originalCacheState = rootView.isDrawingCacheEnabled();
        rootView.setDrawingCacheEnabled(true);
        rootView.buildDrawingCache(true);

        // We could get a null or zero px bitmap if the rootView hasn't been measured
        // appropriately, or we grab it before layout.
        // This is ok, and we should handle it gracefully.
        final Bitmap original = rootView.getDrawingCache();
        Bitmap scaled = null;
        if (null != original && original.getWidth() > 0 && original.getHeight() > 0) {
            if (relativeScaleIfTrue) {
                scaleWidth = original.getWidth() / scaleWidth;
                scaleHeight = original.getHeight() / scaleHeight;
            }
            if (scaleWidth > 0 && scaleHeight > 0) {
                try {
                    scaled = Bitmap.createScaledBitmap(original, scaleWidth, scaleHeight, false);
                } catch (OutOfMemoryError error) {
                    MPLog.i(LOGTAG, "Not enough memory to produce scaled image, returning a null screenshot");
                }
            }
        }
        if (!originalCacheState) {
            rootView.setDrawingCacheEnabled(false);
        }
        return scaled;
    }

    public static int getHighlightColorFromBackground(final Activity activity) {
        int incolor = Color.BLACK;
        final Bitmap screenshot1px = getScaledScreenshot(activity, 1, 1, false);
        if (null != screenshot1px) {
            incolor = screenshot1px.getPixel(0, 0);
        }
        return getHighlightColor(incolor);
    }

    public static int getHighlightColorFromBitmap(final Bitmap bitmap) {
        int incolor = Color.BLACK;
        if (null != bitmap) {
            final Bitmap bitmap1px = Bitmap.createScaledBitmap(bitmap, 1, 1, false);
            incolor = bitmap1px.getPixel(0, 0);
        }
        return getHighlightColor(incolor);
    }

    public static int getHighlightColor(int sampleColor) {
        // Set a constant value level in HSV, in case the averaged color is too light or too dark.
        float[] hsvBackground = new float[3];
        Color.colorToHSV(sampleColor, hsvBackground);
        hsvBackground[2] = 0.3f; // value parameter

        return Color.HSVToColor(0xf2, hsvBackground);
    }

    @SuppressWarnings("unused")
    private static final String LOGTAG = "MixpanelAPI.ActImgUtils";
}

@@ -115,20 +115,95 @@
 aphics.C
-olor
+anvas;%0Aimport android.graphics.Color;%0Aimport android.graphics.drawable.Drawable
 ;%0Aimport
@@ -540,24 +540,25 @@
 RootView();%0A
+%0A
         fina
@@ -559,23 +559,22 @@
   final 
-boolean
+Bitmap
  origina
@@ -578,62 +578,185 @@
 inal
-CacheState = rootView.isDrawingCacheEnabled(
+ = Bitmap.createBitmap(rootView.getWidth() , rootView.getHeight(), Bitmap.Config.ARGB_8888);%0A        Canvas canvas = new Canvas(original
 );%0A
+%0A
        
+ Drawable backgroundDrawable =
  roo
@@ -765,77 +765,210 @@
 iew.
-setDrawingCacheEnabled(true);%0A        rootView.buildDrawingCache(true
+getBackground();%0A        if (backgroundDrawable != null) %7B%0A            backgroundDrawable.draw(canvas);%0A        %7D else %7B%0A            canvas.drawColor(Color.WHITE);%0A        %7D%0A        rootView.draw(canvas
 );%0A%0A
@@ -1171,68 +1171,8 @@
 ly.%0A
-        final Bitmap original = rootView.getDrawingCache();%0A
     
@@ -1811,32 +1811,32 @@
               %7D%0A
+
             %7D%0A  
@@ -1847,105 +1847,8 @@
   %7D%0A
-        if (!originalCacheState) %7B%0A            rootView.setDrawingCacheEnabled(false);%0A        %7D%0A
     

fix crash from getScaledScreenshot due to the deprecated view drawing cache api