<commit_before>/**
 * Copyright 2014 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.droidparts.net.image;

import static android.graphics.Color.TRANSPARENT;
import static org.droidparts.contract.Constants.BUFFER_SIZE;
import static org.droidparts.util.IOUtils.silentlyClose;
import static org.droidparts.util.Strings.isNotEmpty;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.ref.WeakReference;
import java.util.LinkedHashSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

import org.droidparts.concurrent.thread.BackgroundThreadExecutor;
import org.droidparts.contract.HTTP.Header;
import org.droidparts.inner.BitmapFactoryUtils;
import org.droidparts.net.http.HTTPResponse;
import org.droidparts.net.http.RESTClient;
import org.droidparts.net.http.worker.HTTPWorker;
import org.droidparts.net.image.cache.BitmapDiskCache;
import org.droidparts.net.image.cache.BitmapMemoryCache;
import org.droidparts.util.L;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Handler;
import android.os.Looper;
import android.util.Pair;
import android.widget.ImageView;

public class ImageFetcher {

	private final RESTClient restClient;

	private final BitmapMemoryCache memoryCache;
	private final BitmapDiskCache diskCache;

	protected final ThreadPoolExecutor cacheExecutor;
	protected final ThreadPoolExecutor fetchExecutor;

	private final ImageView mockImageView;

	private final LinkedHashSet<ImageViewSpec> pending = new LinkedHashSet<ImageViewSpec>();
	private final ConcurrentHashMap<ImageViewSpec, Long> wip = new ConcurrentHashMap<ImageViewSpec, Long>();
	private Handler handler;

	private volatile boolean paused;

	public ImageFetcher(Context ctx) {
		this(ctx, new BackgroundThreadExecutor(2, "ImageFetcher-Fetch"),
				new RESTClient(ctx), BitmapMemoryCache.getDefaultInstance(ctx),
				BitmapDiskCache.getDefaultInstance(ctx));
	}

	protected ImageFetcher(Context ctx, ThreadPoolExecutor fetchExecutor,
			RESTClient restClient, BitmapMemoryCache memoryCache,
			BitmapDiskCache diskCache) {
		this.fetchExecutor = fetchExecutor;
		this.restClient = restClient;
		this.memoryCache = memoryCache;
		this.diskCache = diskCache;
		handler = new Handler(Looper.getMainLooper());
		cacheExecutor = new BackgroundThreadExecutor(1, "ImageFetcher-Cache");
		mockImageView = new ImageView(ctx.getApplicationContext());
	}

	public void pause() {
		paused = true;
	}

	public void resume(boolean executePendingTasks) {
		paused = false;
		if (executePendingTasks) {
			for (ImageViewSpec spec : pending) {
				ImageView imgView = spec.imgViewRef.get();
				if (imgView != null) {
					attachImage(spec.imgUrl, imgView, spec.crossFadeMillis,
							spec.reshaper, spec.listener,
							spec.inBitmapRef.get());
				}
			}
		}
		pending.clear();
	}

	//

	public void attachImage(String imgUrl, ImageView imageView) {
		attachImage(imgUrl, imageView, 0);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			int crossFadeMillis) {
		attachImage(imgUrl, imageView, null, crossFadeMillis);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			ImageReshaper reshaper, int crossFadeMillis) {
		attachImage(imgUrl, imageView, reshaper, crossFadeMillis, null);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			ImageReshaper reshaper, int crossFadeMillis,
			ImageFetchListener listener) {
		attachImage(imgUrl, imageView, crossFadeMillis, reshaper, listener,
				null);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			int crossFadeMillis, ImageReshaper reshaper,
			ImageFetchListener listener, Bitmap inBitmap) {
		ImageViewSpec spec = new ImageViewSpec(imageView, imgUrl, inBitmap,
				crossFadeMillis, reshaper, listener);
		long submitted = System.nanoTime();
		wip.put(spec, submitted);
		if (paused) {
			pending.remove(spec);
			pending.add(spec);
		} else {
			if (listener != null) {
				listener.onFetchAdded(imageView, imgUrl);
			}
			Runnable r = new ReadFromCacheRunnable(spec, submitted);
			cacheExecutor.remove(r);
			fetchExecutor.remove(r);<commit_msg>ImageFetcher: reordered args in a method.<commit_after>/**
 * Copyright 2014 <NAME>
 * 
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *  
 *     http://www.apache.org/licenses/LICENSE-2.0
 * 
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License. 
 */
package org.droidparts.net.image;

import static android.graphics.Color.TRANSPARENT;
import static org.droidparts.contract.Constants.BUFFER_SIZE;
import static org.droidparts.util.IOUtils.silentlyClose;
import static org.droidparts.util.Strings.isNotEmpty;

import java.io.BufferedInputStream;
import java.io.ByteArrayOutputStream;
import java.lang.ref.WeakReference;
import java.util.LinkedHashSet;
import java.util.concurrent.ConcurrentHashMap;
import java.util.concurrent.ThreadPoolExecutor;

import org.droidparts.concurrent.thread.BackgroundThreadExecutor;
import org.droidparts.contract.HTTP.Header;
import org.droidparts.inner.BitmapFactoryUtils;
import org.droidparts.net.http.HTTPResponse;
import org.droidparts.net.http.RESTClient;
import org.droidparts.net.http.worker.HTTPWorker;
import org.droidparts.net.image.cache.BitmapDiskCache;
import org.droidparts.net.image.cache.BitmapMemoryCache;
import org.droidparts.util.L;

import android.content.Context;
import android.graphics.Bitmap;
import android.graphics.Bitmap.CompressFormat;
import android.graphics.BitmapFactory;
import android.graphics.Point;
import android.graphics.drawable.BitmapDrawable;
import android.graphics.drawable.ColorDrawable;
import android.graphics.drawable.Drawable;
import android.graphics.drawable.TransitionDrawable;
import android.os.Handler;
import android.os.Looper;
import android.util.Pair;
import android.widget.ImageView;

public class ImageFetcher {

	private final RESTClient restClient;

	private final BitmapMemoryCache memoryCache;
	private final BitmapDiskCache diskCache;

	protected final ThreadPoolExecutor cacheExecutor;
	protected final ThreadPoolExecutor fetchExecutor;

	private final ImageView mockImageView;

	private final LinkedHashSet<ImageViewSpec> pending = new LinkedHashSet<ImageViewSpec>();
	private final ConcurrentHashMap<ImageViewSpec, Long> wip = new ConcurrentHashMap<ImageViewSpec, Long>();
	private Handler handler;

	private volatile boolean paused;

	public ImageFetcher(Context ctx) {
		this(ctx, new BackgroundThreadExecutor(2, "ImageFetcher-Fetch"),
				new RESTClient(ctx), BitmapMemoryCache.getDefaultInstance(ctx),
				BitmapDiskCache.getDefaultInstance(ctx));
	}

	protected ImageFetcher(Context ctx, ThreadPoolExecutor fetchExecutor,
			RESTClient restClient, BitmapMemoryCache memoryCache,
			BitmapDiskCache diskCache) {
		this.fetchExecutor = fetchExecutor;
		this.restClient = restClient;
		this.memoryCache = memoryCache;
		this.diskCache = diskCache;
		handler = new Handler(Looper.getMainLooper());
		cacheExecutor = new BackgroundThreadExecutor(1, "ImageFetcher-Cache");
		mockImageView = new ImageView(ctx.getApplicationContext());
	}

	public void pause() {
		paused = true;
	}

	public void resume(boolean executePendingTasks) {
		paused = false;
		if (executePendingTasks) {
			for (ImageViewSpec spec : pending) {
				ImageView imgView = spec.imgViewRef.get();
				if (imgView != null) {
					attachImage(spec.imgUrl, imgView, spec.reshaper,
							spec.crossFadeMillis, spec.listener,
							spec.inBitmapRef.get());
				}
			}
		}
		pending.clear();
	}

	//

	public void attachImage(String imgUrl, ImageView imageView) {
		attachImage(imgUrl, imageView, 0);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			int crossFadeMillis) {
		attachImage(imgUrl, imageView, null, crossFadeMillis);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			ImageReshaper reshaper, int crossFadeMillis) {
		attachImage(imgUrl, imageView, reshaper, crossFadeMillis, null);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			ImageReshaper reshaper, int crossFadeMillis,
			ImageFetchListener listener) {
		attachImage(imgUrl, imageView, reshaper, crossFadeMillis, listener,
				null);
	}

	public void attachImage(String imgUrl, ImageView imageView,
			ImageReshaper reshaper, int crossFadeMillis,
			ImageFetchListener listener, Bitmap inBitmap) {
		ImageViewSpec spec = new ImageViewSpec(imageView, imgUrl, inBitmap,
				crossFadeMillis, reshaper, listener);
		long submitted = System.nanoTime();
		wip.put(spec, submitted);
		if (paused) {
			pending.remove(spec);
			pending.add(spec);
		} else {
			if (listener != null) {
				listener.onFetchAdded(imageView, imgUrl);
			}
			Runnable r = new ReadFromCacheRunnable(spec, submitted);
			cacheExecutor.remove(r);
			fetchExecutor.remove(r);
{'fraction_non_alphanumeric': 0.08909706996212877, 'fraction_numerical': 0.0021925453458241976, 'mean_word_length': 5.31529263687854, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 0, 'lorem ipsum': 0, 'www.': 2, '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': '4936918', 'n_tokens_mistral': 3136, 'n_tokens_neox': 2886, 'n_words': 742}