package com.squareup.picasso;

import android.content.Context;
import android.net.http.HttpResponseCache;
import android.os.Handler;
import android.os.HandlerThread;
import android.os.Process;
import java.io.File;
import java.io.IOException;
import java.net.HttpURLConnection;
import java.net.URL;

public class DefaultHttpLoader implements Loader {

  private static final String PICASSO_CACHE = "picasso-cache";
  private static final int MAX_SIZE = 10 * 1024 * 1024;
  private static final int FLUSH_DELAY = 350;

  private static final Object lock = new Object();
  private static volatile HttpResponseCache cache;

  private final Context context;

  private final FlushCacheRunnable flushCacheRunnable;
  private final HandlerThread purgeThread;
  private Handler purgeThreadHandler;

  public DefaultHttpLoader(Context context) {
    this.context = context.getApplicationContext();
    this.flushCacheRunnable = new FlushCacheRunnable();
    this.purgeThread =
        new HandlerThread("loader-purge-thread", Process.THREAD_PRIORITY_BACKGROUND) {
          @Override protected void onLooperPrepared() {
            purgeThreadHandler = new Handler(purgeThread.getLooper());
          }
        };
    this.purgeThread.start();
  }

  @Override public Response load(String path, boolean allowExpired) throws IOException {
    if (purgeThreadHandler != null) {
      purgeThreadHandler.removeCallbacks(flushCacheRunnable);
    }

    installCacheIfNeeded(context);

    HttpURLConnection connection = (HttpURLConnection) new URL(path).openConnection();
    connection.setUseCaches(true);
    if (allowExpired) {
      connection.setRequestProperty("Cache-Control", "only-if-cached");
    }

    // TODO Should handle this.
    boolean fromCache = false;

    if (purgeThreadHandler != null) {
      purgeThreadHandler.postDelayed(flushCacheRunnable, FLUSH_DELAY);
    }
    return new Response(connection.getInputStream(), fromCache, allowExpired);
  }

  private static void installCacheIfNeeded(Context context) {
    // DCL + volatile should be safe after Java 5.
    if (cache == null) {
      try {
        synchronized (lock) {
          if (cache == null) {
            cache =
                HttpResponseCache.install(new File(context.getCacheDir(), PICASSO_CACHE), MAX_SIZE);
          }
        }
      } catch (IOException ignored) {
      }
    }
  }

  private static final class FlushCacheRunnable implements Runnable {
    @Override public void run() {
      if (cache != null) {
        cache.flush();
      }
    }
  }
}

@@ -103,95 +103,8 @@
 he;%0A
-import android.os.Handler;%0Aimport android.os.HandlerThread;%0Aimport android.os.Process;%0A
 impo
@@ -379,54 +379,8 @@
 024;
-%0A  private static final int FLUSH_DELAY = 350;
 %0A%0A  
@@ -521,786 +521,196 @@
 %0A  p
-rivate final FlushCacheRunnable flushCacheRunnable;%0A  private final HandlerThread purgeThread;%0A  private Handler purgeThreadHandler;%0A%0A  public DefaultHttpLoader(Context context) %7B%0A    this.context = context.getApplicationContext();%0A    this.flushCacheRunnable = new FlushCacheRunnable();%0A    this.purgeThread =%0A        new HandlerThread(%22loader-purge-thread%22, Process.THREAD_PRIORITY_BACKGROUND) %7B%0A          @Override protected void onLooperPrepared() %7B%0A            purgeThreadHandler = new Handler(purgeThread.getLooper());%0A          %7D%0A        %7D;%0A    this.purgeThread.start();%0A  %7D%0A%0A  @Override public Response load(String path, boolean allowExpired) throws IOException %7B%0A    if (purgeThreadHandler != null) %7B%0A      purgeThreadHandler.removeCallbacks(flushCacheRunnable);%0A    %7D%0A
+ublic DefaultHttpLoader(Context context) %7B%0A    this.context = context.getApplicationContext();%0A  %7D%0A%0A  @Override public Response load(String path, boolean allowExpired) throws IOException %7B
 %0A   
@@ -1035,123 +1035,8 @@
 e;%0A%0A
-    if (purgeThreadHandler != null) %7B%0A      purgeThreadHandler.postDelayed(flushCacheRunnable, FLUSH_DELAY);%0A    %7D%0A
     
@@ -1528,180 +1528,7 @@
 %0A  %7D
-%0A%0A  private static final class FlushCacheRunnable implements Runnable %7B%0A    @Override public void run() %7B%0A      if (cache != null) %7B%0A        cache.flush();%0A      %7D%0A    %7D%0A  %7D
 %0A%7D%0A

Remove flushing from Picasso loader.