package com.squareup.picasso;

import android.graphics.Bitmap;
import android.graphics.Canvas;
import android.graphics.ColorFilter;
import android.graphics.LightingColorFilter;
import android.graphics.Paint;
import android.os.Looper;
import java.util.List;

final class Utils {

  private Utils() {
    // No instances.
  }

  static void checkNotMain() {
    if (Looper.getMainLooper().getThread() == Thread.currentThread()) {
      throw new IllegalStateException("Method call should not happen from the main thread.");
    }
  }

  static String createKey(Request request) {
    return createKey(request.path, request.transformations, request.metrics);
  }

  static String createKey(String path, List<Transformation> transformations,
      RequestMetrics metrics) {
    long start = System.nanoTime();
    StringBuilder builder = new StringBuilder();
    builder.append(path);

    if (transformations != null && !transformations.isEmpty()) {
      if (transformations.size() == 1) {
        builder.append('|');
        builder.append(transformations.get(0).toString());
      } else {
        for (Transformation transformation : transformations) {
          builder.append('|');
          builder.append(transformation.toString());
        }
      }
    }

    if (metrics != null) {
      metrics.keyCreationTime = System.nanoTime() - start;
    }

    // TODO Support bitmap options?
    return builder.toString();
  }

  static Bitmap applyDebugColorFilter(Bitmap source, int loadedFrom) {
    int color = RequestMetrics.getColorCodeForCacheHit(loadedFrom);

    ColorFilter filter = new LightingColorFilter(color, 1);

    Paint paint = new Paint();
    paint.setColorFilter(filter);

    Bitmap output = Bitmap.createBitmap(source.getWidth(), source.getHeight(), source.getConfig());

    Canvas canvas = new Canvas(output);
    canvas.drawBitmap(source, 0, 0, paint);

    // Do not recycle source bitmap here. This is the image that is stored inside the cache and can
    // be used again when debugging is off.
    // source.recycle();
    return output;
  }
}

@@ -92,90 +92,8 @@
 as;%0A
-import android.graphics.ColorFilter;%0Aimport android.graphics.LightingColorFilter;%0A
 impo
@@ -1346,206 +1346,244 @@
 %0A%0A  
-static Bitmap applyDebugColorFilter(Bitmap source, int loadedFrom) %7B%0A    int color = RequestMetrics.getColorCodeForCacheHit(loadedFrom);%0A%0A    ColorFilter filter = new LightingColorFilter(color, 1);%0A
+private static final ThreadLocal%3CPaint%3E DEBUG_PAINT = new ThreadLocal%3CPaint%3E() %7B%0A    @Override protected Paint initialValue() %7B%0A      return new Paint();%0A    %7D%0A  %7D;%0A%0A  static Bitmap applyDebugColorFilter(Bitmap source, int loadedFrom) %7B
 %0A   
@@ -1593,64 +1593,40 @@
 int 
-p
+debugP
 aint = 
-new Paint();%0A    paint.setColorFilter(filter
+DEBUG_PAINT.get(
 );%0A
-%0A
     
@@ -1798,17 +1798,369 @@
 , 0, 0, 
-p
+null);%0A    canvas.rotate(45);%0A%0A    // Draw a white square for the indicator border.%0A    debugPaint.setColor(0xFFFFFFFF);%0A    canvas.drawRect(0, -20, 15, 20, debugPaint);%0A%0A    // Draw a slightly smaller square for the indicator color.%0A    debugPaint.setColor(RequestMetrics.getColorCodeForCacheHit(loadedFrom));%0A    canvas.drawRect(0, -18, 13, 18, debugP
 aint);%0A%0A
@@ -2307,32 +2307,8 @@
 ff.%0A
-    // source.recycle();
 %0A   

Use triangle in upper-left for source indication.