aboutsummaryrefslogtreecommitdiff
path: root/src/org/linaro/connect/JSONImageListAdapter.java
blob: 8c2715d1ade462b852443f929a1b52a14f2b788d (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
package org.linaro.connect;

import java.lang.ref.SoftReference;
import java.util.HashMap;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

import android.content.Context;
import android.graphics.drawable.Drawable;
import android.os.Handler;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ArrayAdapter;
import android.widget.GridView;
import android.widget.ImageView;

public class JSONImageListAdapter extends ArrayAdapter<JSONImageItem> {

	private final Handler mHandler;
	private final BlockingQueue<JSONImageItem> mItemQueue;
	private final HashMap<JSONImageItem,SoftReference<Drawable>> mCache;

	private boolean mWorking = true;

	public JSONImageListAdapter(Context ctx) {
		super(ctx, 0, 0);

		mCache = new HashMap<JSONImageItem,SoftReference<Drawable>>();
		mItemQueue = new LinkedBlockingQueue<JSONImageItem>();
		mHandler = new Handler();
		new Thread(mDownloader).start();
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ImageView imageView;
		if (convertView == null) { // if it's not recycled, initialize some
									// attributes
			imageView = new ImageView(getContext());
			imageView.setLayoutParams(new GridView.LayoutParams(85, 85));
			imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
			imageView.setPadding(8, 8, 8, 8);
		} else {
			imageView = (ImageView) convertView;
		}

		JSONImageItem jso = getItem(position);
		SoftReference<Drawable> ref = mCache.get(jso);
		if( ref != null ) {
			Drawable d = ref.get();
			if( d == null ) { //we've been garbage collected, read from cache
				d = jso.getThumnail();
				mCache.put(jso, new SoftReference<Drawable>(d));
			}

			imageView.setImageDrawable(d);
		} else {
			imageView.setImageResource(R.drawable.image_loading);
		}
		return imageView;
	}

	@Override
	public void add(JSONImageItem jso) {
		super.add(jso);
		mItemQueue.add(jso);
	}

	public void setLastItem() {
		mWorking = false;
	}

	private void downloadAndDisplay(JSONImageItem jsi) {
		final Drawable d = jsi.getThumnail();
		if ( d != null ) {
			mCache.put(jsi, new SoftReference<Drawable>(d));
			mHandler.post(new Runnable() {
				@Override
				public void run() {
					notifyDataSetChanged();
				}
			});
		}
		else {
			Log.e(LinaroConnect.TAG, "null jso or view in image list");
			mItemQueue.add(jsi); //try again for later
		}
	}

	private final Runnable mDownloader = new Runnable() {
		@Override
		public void run() {
			while ( mWorking || !mItemQueue.isEmpty()) {
				try {
					JSONImageItem jsi = mItemQueue.take();
					downloadAndDisplay(jsi);
				} catch (Exception e) {
					Log.e(LinaroConnect.TAG, "error getting image", e);
				}
			}

		}
	};
}