<commit_before>// GalleryActivity.java
// twixelated image gallery
// TwixelCam - Copyright © 2016 <NAME>


package com.ionoclast.twixelcam.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.AsyncTask;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ionoclast.twixelcam.CameraTwixelator;
import com.ionoclast.twixelcam.R;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @author btoskin &lt;<EMAIL>&gt; Ionoclast Laboratories, LLC.
 */
public class GalleryActivity extends Activity
{
	ViewPager mGalTwixelated;
	File[] mFiles;
	Bitmap[] mImages;

	ThreadPoolExecutor mExecutor;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);

		mGalTwixelated = (ViewPager)findViewById(R.id.lst_gallery);

		mFiles = listFiles();
		mImages = new Bitmap[mFiles.length];
		mExecutor = new ThreadPoolExecutor(1, 4, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(4));
		new LoadBitmapsTask(0, 4).executeOnExecutor(mExecutor);

		mGalTwixelated.setAdapter(new TwixelatedPageAdapter());
	}

	private File[] listFiles()
	{
		File[] tFiles = CameraTwixelator.TWIXEL_DIR.listFiles();
		Arrays.sort(tFiles, new Comparator<File>()
		{
			@Override
			public int compare(File pLhs, File pRhs)
			{
				// sort newest first
				return Long.valueOf(pRhs.lastModified()).compareTo(pLhs.lastModified());
			}
		});
		return tFiles;
	}

	private class LoadBitmapsTask extends AsyncTask<Void, Void, Void>
	{
		private int mStartIndex, mCount;

		public LoadBitmapsTask(int pStart, int pCount)
		{
			mStartIndex = pStart;
			mCount = pCount;
		}

		@Override
		protected Void doInBackground(Void... pParams)
		{
			for(int i = mStartIndex; i < mStartIndex + mCount; ++i)
			{
				synchronized(mImages)
				{
					if(mImages[i] == null)
					{
						String tFile = mFiles[i].toString();
						mImages[i] = BitmapFactory.decodeFile(tFile);
					}
				}
			}

			return null;
		}

		@Override
		protected void onPostExecute(Void pVoid)
		{
			// ???
		}
	}

	private class TwixelatedPageAdapter extends PagerAdapter
	{
		@Override
		public int getCount()
		{
			return mImages.length;
		}

		@Override
		public Object instantiateItem(ViewGroup pParent, int pPosition)
		{
			ImageView tImageView = new ImageView(GalleryActivity.this);
			tImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

			synchronized(mImages)
			{
				if(mImages[pPosition] == null)
				{
					// load this one on demand, and queue up the next few on a thread
					mImages[pPosition] = BitmapFactory.decodeFile(mFiles[pPosition].toString());
					int tNumToLoad = Math.min(mFiles.length - pPosition - 1, 4);
					if(tNumToLoad > 0)
					{
						new LoadBitmapsTask(pPosition + 1, tNumToLoad).executeOnExecutor(mExecutor);
					}
				}
			}
			tImageView.setImageBitmap(mImages[pPosition]);<commit_msg>Convert AsyncTask to simple Runnable.<commit_after>// GalleryActivity.java
// twixelated image gallery
// TwixelCam - Copyright © 2016 <NAME>


package com.ionoclast.twixelcam.activity;

import android.app.Activity;
import android.graphics.Bitmap;
import android.graphics.BitmapFactory;
import android.os.Bundle;
import android.support.v4.view.PagerAdapter;
import android.support.v4.view.ViewPager;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;

import com.ionoclast.twixelcam.CameraTwixelator;
import com.ionoclast.twixelcam.R;

import java.io.File;
import java.util.Arrays;
import java.util.Comparator;
import java.util.Queue;
import java.util.concurrent.ArrayBlockingQueue;
import java.util.concurrent.ThreadPoolExecutor;
import java.util.concurrent.TimeUnit;


/**
 * @author btoskin &lt;<EMAIL>&gt; Ionoclast Laboratories, LLC.
 */
public class GalleryActivity extends Activity
{
	ViewPager mGalTwixelated;
	File[] mFiles;
	Bitmap[] mImages;

	ThreadPoolExecutor mExecutor;

	@Override
	protected void onCreate(Bundle savedInstanceState)
	{
		super.onCreate(savedInstanceState);
		setContentView(R.layout.gallery);

		mGalTwixelated = (ViewPager)findViewById(R.id.lst_gallery);

		mFiles = listFiles();
		mImages = new Bitmap[mFiles.length];
		mExecutor = new ThreadPoolExecutor(1, 4, 1000, TimeUnit.MILLISECONDS, new ArrayBlockingQueue<Runnable>(4));
		mExecutor.execute(new LoadBitmapsTask(0, 4));

		mGalTwixelated.setAdapter(new TwixelatedPageAdapter());
	}

	private File[] listFiles()
	{
		File[] tFiles = CameraTwixelator.TWIXEL_DIR.listFiles();
		Arrays.sort(tFiles, new Comparator<File>()
		{
			@Override
			public int compare(File pLhs, File pRhs)
			{
				// sort newest first
				return Long.valueOf(pRhs.lastModified()).compareTo(pLhs.lastModified());
			}
		});
		return tFiles;
	}

	private class LoadBitmapsTask implements Runnable
	{
		private int mStartIndex, mCount;

		public LoadBitmapsTask(int pStart, int pCount)
		{
			mStartIndex = pStart;
			mCount = pCount;
		}

		@Override
		public void run()
		{
			for(int i = mStartIndex; i < mStartIndex + mCount; ++i)
			{
				synchronized(mImages)
				{
					if(mImages[i] == null)
					{
						String tFile = mFiles[i].toString();
						mImages[i] = BitmapFactory.decodeFile(tFile);
					}
				}
			}
		}
	}

	private class TwixelatedPageAdapter extends PagerAdapter
	{
		private Queue<ImageView> mRecycledViews;

		@Override
		public int getCount()
		{
			return mImages.length;
		}

		@Override
		public Object instantiateItem(ViewGroup pParent, int pPosition)
		{
			ImageView tImageView = new ImageView(GalleryActivity.this);
			tImageView.setScaleType(ImageView.ScaleType.FIT_CENTER);

			synchronized(mImages)
			{
				if(mImages[pPosition] == null)
				{
					// load this one on demand, and queue up the next few on a thread
					mImages[pPosition] = BitmapFactory.decodeFile(mFiles[pPosition].toString());
					int tNumToLoad = Math.min(mFiles.length - pPosition - 1, 4);
					if(tNumToLoad > 0)
					{
						mExecutor.execute(new LoadBitmapsTask(pPosition + 1, tNumToLoad));
					}
				}
			}
			tImageView.setImageBitmap(mImages[pPosition]);
{'fraction_non_alphanumeric': 0.1099891253689607, 'fraction_numerical': 0.005903371135622185, 'mean_word_length': 4.1794046661303295, 'pattern_counts': {'":': 0, '<': 15, '<?xml version=': 0, '>': 15, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, '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': '3229530', 'n_tokens_mistral': 2490, 'n_tokens_neox': 2097, 'n_words': 434}