diff --git a/whatsup/src/main/java/com/whatsup/whatsup/GVImageLoader.java b/whatsup/src/main/java/com/whatsup/whatsup/GVImageLoader.java deleted file mode 100644 index 32d819e..0000000 --- a/whatsup/src/main/java/com/whatsup/whatsup/GVImageLoader.java +++ /dev/null @@ -1,73 +0,0 @@ -package com.whatsup.whatsup; - -import android.app.Activity; -import android.content.Context; -import android.graphics.Bitmap; -import android.graphics.BitmapFactory; -import android.os.AsyncTask; -import android.util.Log; -import android.widget.ImageView; - -import java.io.File; - -/** - * Created by alenin on 28/07/2014. - */ - -public class GVImageLoader extends Thread { - private ViewHolder_GVItem mVh; - private String mAddr; - private int mPosition; - private Activity mContext; - - public GVImageLoader(Activity context, String pathFile, ViewHolder_GVItem vh, int position ) { - this.mContext = context; - this.mAddr = pathFile; - this.mVh = vh; - this.mPosition = position; - run(); - } - - @Override - public void run() { - if (this.mVh.position == this.mPosition) { - mVh.icon.setImageBitmap(BitmapFactory.decodeFile(mAddr)); - /*this.mContext.runOnUiThread(new Runnable() { - @Override - public void run() { - - } - });*/ - } - } -} -/* -public class GVImageLoader extends AsyncTask { - private ImageView iv; - private ViewHolder_GVItem vh; - private String addr; - private int position; - - public GVImageLoader( String pathFile, ViewHolder_GVItem vh, int position ) { - addr = pathFile; - //this.iv = iv; - this.vh = vh; - this.position = position; - execute(); - } - - @Override - protected Bitmap doInBackground(ViewHolder_GVItem... viewHolders) { - Log.d("doInBackground", "on GVImageLoader"); - return BitmapFactory.decodeFile(addr); - } - - @Override - protected void onPostExecute( Bitmap result ) { - Log.d("onPostExecute", "on GVImageLoader"); - - if( this.vh.position == this.position ) - this.vh.icon.setImageBitmap( result ); - } -}*/ - diff --git a/whatsup/src/main/java/com/whatsup/whatsup/ImageLoader.java b/whatsup/src/main/java/com/whatsup/whatsup/ImageLoader.java index 9fc237b..354648d 100644 --- a/whatsup/src/main/java/com/whatsup/whatsup/ImageLoader.java +++ b/whatsup/src/main/java/com/whatsup/whatsup/ImageLoader.java @@ -1,122 +1,95 @@ package com.whatsup.whatsup; +import android.app.Activity; +import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; -import android.os.Handler; -import android.os.Looper; +import android.graphics.drawable.Drawable; +import android.os.AsyncTask; +import android.support.v4.util.LruCache; import android.util.Log; import android.widget.ImageView; -import android.widget.ViewSwitcher; -public class ImageLoader extends Thread { - - public interface ImageLoadListener { - - void handleImageLoaded(ViewSwitcher aViewSwitcher, ImageView aImageView, Bitmap aBitmap); - } - - private static final String TAG = ImageLoader.class.getSimpleName(); - ImageLoadListener mListener = null; - private Handler handler; - - /** - * Image loader takes an object that extends ImageLoadListener - * @param lListener - */ - ImageLoader(ImageLoadListener lListener){ - mListener = lListener; +import java.io.File; +import java.util.ArrayList; +import java.util.List; +import java.util.concurrent.Executor; +import java.util.concurrent.Executors; + +/** + * Created by alenin on 28/07/2014. + */ +/* +public class GVImageLoader extends Thread { + private ViewHolder_GVItem mVh; + private String mAddr; + private int mPosition; + private Activity mContext; + + public GVImageLoader(Activity context, String pathFile, ViewHolder_GVItem vh, int position ) { + this.mContext = context; + this.mAddr = pathFile; + this.mVh = vh; + this.mPosition = position; + run(); } @Override public void run() { - try { - -// preparing a looper on current thread -// the current thread is being detected implicitly - Looper.prepare(); + if (this.mVh.position == this.mPosition) { + mVh.icon.setImageBitmap(BitmapFactory.decodeFile(mAddr)); -// Looper gets attached to the current thread by default - handler = new Handler(); - - Looper.loop(); -// Thread will start - - } catch (Throwable t) { - Log.e(TAG, "ImageLoader halted due to a error: ", t); - } + } } - - /** - * Method stops the looper and thus the thread - */ - public synchronized void stopThread() { - -// Use the handler to schedule a quit on the looper - handler.post(new Runnable() { - - public void run() { -// This runs on the ImageLoader thread - Log.i(TAG, "DownloadThread loop quitting by request"); - - Looper.myLooper().quit(); +}*/ + +public class ImageLoader implements ImageLoaderAsyncTask.OnImageLoaderAsyncTaskListener { + private LruCache mMemoryCache; + private Executor mImageLoaderExecutor; + private List< ImageLoaderAsyncTask > mAsyncTask = new ArrayList(); + private int mNoLoadedImage; + + public ImageLoader(int mNoLoadedImage_in) { + mImageLoaderExecutor = Executors.newSingleThreadExecutor(); + this.mNoLoadedImage = mNoLoadedImage_in; + final int maxMemory = (int) (Runtime.getRuntime().maxMemory() / 1024); + // Use 1/8th of the available memory for this memory cache. + final int cacheSize = maxMemory / 2; + mMemoryCache = new LruCache(cacheSize) { + @Override + protected int sizeOf(String key, Bitmap bitmap) { + // The cache size will be measured in kilobytes rather than + // number of items. + return bitmap.getByteCount() / 1024; } - }); + }; } - /** - * Method queues the image at path to load - * Note that the actual loading takes place in the UI thread - * the ImageView and ViewSwitcher are just references for the - * UI thread. - * @param aPath - Path where the bitmap is located to load - * @param aImageView - The ImageView the UI thread will load - * @param aViewSwitcher - The ViewSwitcher that needs to display the imageview - */ - public synchronized void queueImageLoad( - final String aPath, - final ImageView aImageView, - final ViewSwitcher aViewSwitcher) { -// Wrap DownloadTask into another Runnable to track the statistics - handler.post(new Runnable() { - public void run() { - try { - synchronized (aImageView){ -// make sure this thread is the only one performing activities on -// this imageview - BitmapFactory.Options lOptions = new BitmapFactory.Options(); - lOptions.inSampleSize = 1; - Bitmap lBitmap = BitmapFactory.decodeFile(aPath, lOptions); -//aImage.setImageBitmap(lBitmap); - -// Load the image here - signalUI(aViewSwitcher, aImageView, lBitmap); - } - } - catch(Exception e){ - e.printStackTrace(); - } - } - }); + public Bitmap getBitmapFromMemCache(String key) { + return mMemoryCache.get(key); } - /** - * Method is called when the bitmap is loaded. The UI thread adds the bitmap to the imageview. - * @param aViewSwitcher - The ViewSwitcher that needs to display the imageview - * @param aImageView - The ImageView the UI thread will load - * @param aImage - The Bitmap that gets loaded into the ImageView - */ - private void signalUI( - ViewSwitcher aViewSwitcher, - ImageView aImageView, - Bitmap aImage){ - - if(mListener != null){ -// we have an object that implements ImageLoadListener - - mListener.handleImageLoaded(aViewSwitcher, aImageView, aImage); + public void addBitmapToMemoryCache(String key, Bitmap bitmap) { + if (getBitmapFromMemCache(key) == null) { + mMemoryCache.put(key, bitmap); } } -} \ No newline at end of file + public void getImage( String pathFile_in, int position_in, ViewHolder_GVItem vh_in, Executor pool ) { + Bitmap bitmap = null; + + bitmap = getBitmapFromMemCache(String.valueOf(position_in)); + if (bitmap != null) { + Log.d("cached", "yes"); + vh_in.icon.setImageBitmap( bitmap ); + } else { + Log.d("cached", "no"); + vh_in.icon.setImageResource( mNoLoadedImage ); + if( pathFile_in != null) + new ImageLoaderAsyncTask( vh_in, position_in, pathFile_in, mImageLoaderExecutor, this ); + } + } +} + diff --git a/whatsup/src/main/java/com/whatsup/whatsup/ImageLoaderAsyncTask.java b/whatsup/src/main/java/com/whatsup/whatsup/ImageLoaderAsyncTask.java new file mode 100644 index 0000000..70fbf37 --- /dev/null +++ b/whatsup/src/main/java/com/whatsup/whatsup/ImageLoaderAsyncTask.java @@ -0,0 +1,45 @@ +package com.whatsup.whatsup; + +import android.graphics.Bitmap; +import android.graphics.BitmapFactory; +import android.os.AsyncTask; +import android.util.Log; + +import java.util.concurrent.Executor; + +/** + * Created by alenin on 01/08/2014. + */ +public class ImageLoaderAsyncTask extends AsyncTask { + + public interface OnImageLoaderAsyncTaskListener { + public void addBitmapToMemoryCache(String key, Bitmap bitmap); + } + + OnImageLoaderAsyncTaskListener mCallBback; + private ViewHolder_GVItem vh; + private int position; + private String addr; + + public ImageLoaderAsyncTask( ViewHolder_GVItem vh, int position, String pathFile, Executor pool, ImageLoader callbackclass) { + this.vh = vh; + this.position = position; + this.addr = pathFile; + mCallBback = (OnImageLoaderAsyncTaskListener) callbackclass; + execute(); + } + + protected Bitmap doInBackground(Integer... positions) { + Log.d("doInBackground", "on ImageLoaderAsyncTask"); + return BitmapFactory.decodeFile( this.addr ); + } + + @Override + protected void onPostExecute( Bitmap result ) { + Log.d("onPostExecute", "on ImageLoaderAsyncTask"); + if( this.vh.position == this.position ) { + this.vh.icon.setImageBitmap(result); + mCallBback.addBitmapToMemoryCache( String.valueOf( vh.position ), result ); + } + } +} diff --git a/whatsup/src/main/java/com/whatsup/whatsup/WhatWasHereFragmentGV.java b/whatsup/src/main/java/com/whatsup/whatsup/WhatWasHereFragmentGV.java index 08f1270..e0a6ac7 100644 --- a/whatsup/src/main/java/com/whatsup/whatsup/WhatWasHereFragmentGV.java +++ b/whatsup/src/main/java/com/whatsup/whatsup/WhatWasHereFragmentGV.java @@ -2,12 +2,10 @@ import android.app.Activity; import android.app.Fragment; -import android.app.ListFragment; import android.content.Context; import android.graphics.Bitmap; import android.graphics.BitmapFactory; import android.graphics.Point; -import android.graphics.drawable.Drawable; import android.os.AsyncTask; import android.os.Bundle; import android.os.Handler; @@ -18,11 +16,9 @@ import android.view.View; import android.view.ViewGroup; import android.widget.AdapterView; -import android.widget.ArrayAdapter; import android.widget.BaseAdapter; import android.widget.GridView; import android.widget.ImageView; -import android.widget.LinearLayout; import android.widget.RelativeLayout; import android.widget.TextView; import android.widget.Toast; @@ -32,16 +28,13 @@ import org.json.JSONException; import org.json.JSONObject; +import java.io.BufferedInputStream; import java.io.File; import java.io.FileOutputStream; import java.io.InputStream; import java.net.HttpURLConnection; import java.net.URL; -import java.text.DateFormat; -import java.text.ParseException; -import java.text.SimpleDateFormat; import java.util.ArrayList; -import java.util.Date; import java.util.HashMap; import java.util.List; import java.util.concurrent.Executor; @@ -76,6 +69,7 @@ public class WhatWasHereFragmentGV extends Fragment { private ImageAdapter imgAdapter; private ExecutorService service; private ExecutorService mImageLoaderPoolThread; + private Executor mImageLoaderExecutor; public interface OnWwhGvsFragmentListener { public void setCurrentFragmentTag(String tag); @@ -350,7 +344,7 @@ public void run() { try { File cacheDirectory = getActivity().getCacheDir(); tmpFile = new File( cacheDirectory.getPath() + "/" + urls[2] + "_" + urls[1] ); - if( !tmpFile.exists() ) { + if( true ) { Log.d("Image was on cache", "no"); url = new URL(imgUrl); HttpURLConnection urlConnection = (HttpURLConnection) url.openConnection(); @@ -358,9 +352,10 @@ public void run() { if( isInterrupted() ) return; iStream = urlConnection.getInputStream(); FileOutputStream fOutStream = new FileOutputStream(tmpFile); - Bitmap b = BitmapFactory.decodeStream(iStream); + Bitmap b = BitmapFactory.decodeStream( iStream ); + b = Bitmap.createScaledBitmap(b, 150, 150, false); if( isInterrupted() ) return; - b.compress(Bitmap.CompressFormat.JPEG, 30, fOutStream); + b.compress(Bitmap.CompressFormat.JPEG, 70, fOutStream); fOutStream.flush(); fOutStream.close(); }else { @@ -380,6 +375,29 @@ public void run() { } } + + public int calculateInSampleSize( + BitmapFactory.Options options, int reqWidth, int reqHeight) { + // Raw height and width of image + final int height = options.outHeight; + final int width = options.outWidth; + int inSampleSize = 1; + + if (height > reqHeight || width > reqWidth) { + + final int halfHeight = height / 2; + final int halfWidth = width / 2; + + // Calculate the largest inSampleSize value that is a power of 2 and keeps both + // height and width larger than the requested height and width. + while ((halfHeight / inSampleSize) > reqHeight + && (halfWidth / inSampleSize) > reqWidth) { + inSampleSize *= 2; + } + } + + return inSampleSize; + } } /* private class ImageLoaderTask extends AsyncTask { @@ -436,7 +454,6 @@ private class ImageAdapter extends BaseAdapter{ private int mQuantity; private int hw; private HashMap mImages = new HashMap(); - private LruCache mMemoryCache; private static final int PROGRESSBARINDEX = 0; private static final int IMAGEVIEWINDEX = 1; @@ -452,22 +469,13 @@ public ImageAdapter(Context mContext, int resourceId, int quantity) { for( int i=0; i(cacheSize) { - @Override - protected int sizeOf(String key, Bitmap bitmap) { - // The cache size will be measured in kilobytes rather than - // number of items. - return bitmap.getByteCount() / 1024; - } - }; + Display display = getActivity().getWindowManager().getDefaultDisplay(); Point size = new Point(); display.getSize(size); hw = size.x / 3; - mImageLoaderPoolThread = Executors.newCachedThreadPool(); + mImageLoader = new ImageLoader( R.drawable.empty_frame ); + } public int getCount() { @@ -505,27 +513,10 @@ public View getView(int position, View convertView, ViewGroup parent) { } else { vh = (ViewHolder_GVItem) convertView.getTag(); //vh = new ViewHolder(); - vh.icon.setImageResource(R.drawable.empty_frame); + vh.icon.setImageResource( R.drawable.empty_frame); vh.position = position; } - if( mImages.get(position) == null ) { - //vh.icon.setImageResource(R.drawable.empty_frame); - }else { - //vh.icon.setImageResource( R.drawable.empty_frame ); - mImageLoaderPoolThread.submit( new GVImageLoader(getActivity(), mImages.get( position ), vh, position ) ); - /*Bitmap bitmap = getBitmapFromMemCache(String.valueOf(position)); - if (bitmap != null) { - imageView.setImageBitmap(bitmap); - Log.d("cached", "yes"); - } else { - addBitmapToMemoryCache(String.valueOf(position), BitmapFactory.decodeFile(mImages.get(position))); - bitmap = getBitmapFromMemCache(String.valueOf(position)); - imageView.setImageBitmap(bitmap); - Log.d("cached", "no"); - } - //imageView.setImageDrawable( Drawable.createFromPath( mImages.get(position) ) );*/ - } - + mImageLoader.getImage( mImages.get( position ), position, vh, null ); return convertView; } @@ -534,15 +525,7 @@ public void setImage( int position, String path ) { Log.d("setImage" , "yes"); } - public Bitmap getBitmapFromMemCache(String key) { - return mMemoryCache.get(key); - } - public void addBitmapToMemoryCache(String key, Bitmap bitmap) { - if (getBitmapFromMemCache(key) == null) { - mMemoryCache.put(key, bitmap); - } - } public void handleImageLoaded( final ViewSwitcher aViewSwitcher, final ImageView aImageView,