/*
 * Copyright (c) 2015-present, Facebook, Inc.
 * All rights reserved.
 *
 * This source code is licensed under the BSD-style license found in the
 * LICENSE file in the root directory of this source tree. An additional grant
 * of patent rights can be found in the PATENTS file in the same directory.
 */

package com.facebook.imagepipeline.producers;

import java.io.InputStream;
import java.net.HttpURLConnection;
import java.net.URL;
import java.util.concurrent.Executor;
import java.util.concurrent.Executors;

import android.net.Uri;

import com.facebook.common.references.CloseableReference;
import com.facebook.imagepipeline.memory.PooledByteBuffer;

/**
 * Network fetcher that uses the simplest Android stack.
 *
 * <p> Apps requiring more sophisticated networking should implement their own
 * {@link NetworkFetcher}.
 */
public class HttpUrlConnectionNetworkFetcher extends BaseNetworkFetcher<FetchState> {

  private static final int NUM_NETWORK_THREADS = 3;

  private final Executor mExecutor;

  public HttpUrlConnectionNetworkFetcher() {
    mExecutor = Executors.newFixedThreadPool(NUM_NETWORK_THREADS);
  }

  @Override
  public FetchState createFetchState(
      Consumer<CloseableReference<PooledByteBuffer>> consumer,
      ProducerContext context) {
    return new FetchState(consumer, context);
  }

  @Override
  public void fetch(final FetchState fetchState, final Callback callback) {
    mExecutor.execute(
        new Runnable() {
          @Override
          public void run() {
            HttpURLConnection connection = null;
            try {
              Uri uri = fetchState.getUri();
              URL url = new URL(uri.toString());
              connection = (HttpURLConnection) url.openConnection();
              InputStream is = connection.getInputStream();
              callback.onResponse(is, -1);
            } catch (Exception e) {
              callback.onFailure(e);
            } finally {
              if (connection != null) {
                connection.disconnect();
              }
            }
          }
        });
  }
}

@@ -345,24 +345,167 @@
 producers;%0A%0A
+import android.net.Uri;%0A%0Aimport com.facebook.common.references.CloseableReference;%0Aimport com.facebook.imagepipeline.memory.PooledByteBuffer;%0A%0A
 import java.
@@ -612,16 +612,23 @@
 Executor
+Service
 ;%0Aimport
@@ -664,149 +664,42 @@
 rs;%0A
-%0A
 import 
-android.net.Uri;%0A%0Aimport com.facebook.common.references.CloseableReference;%0Aimport com.facebook.imagepipeline.memory.PooledByteBuffer
+java.util.concurrent.Future
 ;%0A%0A/
@@ -1035,16 +1035,23 @@
 Executor
+Service
  mExecut
@@ -1052,16 +1052,23 @@
 Executor
+Service
 ;%0A%0A  pub
@@ -1120,16 +1120,23 @@
 Executor
+Service
  = Execu
@@ -1479,25 +1479,56 @@
     
-mExecutor.execute
+final Future%3C?%3E future = mExecutorService.submit
 (%0A  
@@ -2165,14 +2165,290 @@
     %7D);%0A
+    fetchState.getContext().addCallbacks(%0A        new BaseProducerContextCallbacks() %7B%0A          @Override%0A          public void onCancellationRequested() %7B%0A            if (future.cancel(false)) %7B%0A              callback.onCancellation();%0A            %7D%0A          %7D%0A        %7D);%0A
   %7D%0A%7D%0A

Add cancellation to default network fetcher