Skip to content

HTTPS clone URL

Subversion checkout URL

You can clone with HTTPS or Subversion.

Download ZIP
Browse files

gridview kinda works

  • Loading branch information...
commit e74bb94a5fb9d24acb53a9880bd0f80e0f0ccf3a 1 parent 2c8db7b
@markchang authored
View
1  AndroidManifest.xml
@@ -25,6 +25,7 @@
</intent-filter>
</activity>
<activity android:name=".ImageListActivity"/>
+ <activity android:name=".ImageGridActivity"/>
<activity android:name=".DashboardActivity"/>
</application>
</manifest>
View
46 gen/com/markupartist/android/widget/actionbar/R.java
@@ -82,40 +82,42 @@ theme attribute (in the form
public static final int actionbar_progress=0x7f090007;
public static final int actionbar_title=0x7f090008;
public static final int activity_button=0x7f09000b;
- public static final int btnCamera=0x7f09001b;
- public static final int btnGallery=0x7f09001c;
- public static final int btnLogin=0x7f090019;
- public static final int btnUpload=0x7f09001d;
+ public static final int btnCamera=0x7f09001c;
+ public static final int btnGallery=0x7f09001d;
+ public static final int btnLogin=0x7f09001a;
+ public static final int btnUpload=0x7f09001e;
public static final int camera_button=0x7f09000e;
- public static final int caption=0x7f090015;
- public static final int captionRow=0x7f09001e;
- public static final int clear=0x7f090022;
- public static final int comments=0x7f090016;
+ public static final int caption=0x7f090016;
+ public static final int captionRow=0x7f09001f;
+ public static final int clear=0x7f090023;
+ public static final int comments=0x7f090017;
public static final int dashboardActionbar=0x7f09000a;
public static final int gallery_button=0x7f09000f;
- public static final int image=0x7f090014;
- public static final int imageListActionbar=0x7f090011;
- public static final int imageView=0x7f090021;
- public static final int lblCaption=0x7f09001f;
- public static final int list=0x7f090012;
+ public static final int gridview=0x7f090011;
+ public static final int image=0x7f090015;
+ public static final int imageListActionbar=0x7f090012;
+ public static final int imageView=0x7f090022;
+ public static final int lblCaption=0x7f090020;
+ public static final int list=0x7f090013;
public static final int login_button=0x7f090010;
- public static final int pictureActionbar=0x7f09001a;
+ public static final int pictureActionbar=0x7f09001b;
public static final int popular_button=0x7f09000c;
public static final int screen=0x7f090000;
- public static final int txtCaption=0x7f090020;
- public static final int txtPassword=0x7f090018;
- public static final int txtUsername=0x7f090017;
+ public static final int txtCaption=0x7f090021;
+ public static final int txtPassword=0x7f090019;
+ public static final int txtUsername=0x7f090018;
public static final int user_feed_button=0x7f09000d;
- public static final int username=0x7f090013;
+ public static final int username=0x7f090014;
}
public static final class layout {
public static final int actionbar=0x7f030000;
public static final int actionbar_item=0x7f030001;
public static final int dashboard_home=0x7f030002;
- public static final int image_list=0x7f030003;
- public static final int image_list_item=0x7f030004;
- public static final int login=0x7f030005;
- public static final int take_picture=0x7f030006;
+ public static final int image_grid=0x7f030003;
+ public static final int image_list=0x7f030004;
+ public static final int image_list_item=0x7f030005;
+ public static final int login=0x7f030006;
+ public static final int take_picture=0x7f030007;
}
public static final class menu {
public static final int take_picture_menu=0x7f080000;
View
46 gen/org/acmelab/andgram/R.java
@@ -82,40 +82,42 @@ theme attribute (in the form
public static final int actionbar_progress=0x7f090007;
public static final int actionbar_title=0x7f090008;
public static final int activity_button=0x7f09000b;
- public static final int btnCamera=0x7f09001b;
- public static final int btnGallery=0x7f09001c;
- public static final int btnLogin=0x7f090019;
- public static final int btnUpload=0x7f09001d;
+ public static final int btnCamera=0x7f09001c;
+ public static final int btnGallery=0x7f09001d;
+ public static final int btnLogin=0x7f09001a;
+ public static final int btnUpload=0x7f09001e;
public static final int camera_button=0x7f09000e;
- public static final int caption=0x7f090015;
- public static final int captionRow=0x7f09001e;
- public static final int clear=0x7f090022;
- public static final int comments=0x7f090016;
+ public static final int caption=0x7f090016;
+ public static final int captionRow=0x7f09001f;
+ public static final int clear=0x7f090023;
+ public static final int comments=0x7f090017;
public static final int dashboardActionbar=0x7f09000a;
public static final int gallery_button=0x7f09000f;
- public static final int image=0x7f090014;
- public static final int imageListActionbar=0x7f090011;
- public static final int imageView=0x7f090021;
- public static final int lblCaption=0x7f09001f;
- public static final int list=0x7f090012;
+ public static final int gridview=0x7f090011;
+ public static final int image=0x7f090015;
+ public static final int imageListActionbar=0x7f090012;
+ public static final int imageView=0x7f090022;
+ public static final int lblCaption=0x7f090020;
+ public static final int list=0x7f090013;
public static final int login_button=0x7f090010;
- public static final int pictureActionbar=0x7f09001a;
+ public static final int pictureActionbar=0x7f09001b;
public static final int popular_button=0x7f09000c;
public static final int screen=0x7f090000;
- public static final int txtCaption=0x7f090020;
- public static final int txtPassword=0x7f090018;
- public static final int txtUsername=0x7f090017;
+ public static final int txtCaption=0x7f090021;
+ public static final int txtPassword=0x7f090019;
+ public static final int txtUsername=0x7f090018;
public static final int user_feed_button=0x7f09000d;
- public static final int username=0x7f090013;
+ public static final int username=0x7f090014;
}
public static final class layout {
public static final int actionbar=0x7f030000;
public static final int actionbar_item=0x7f030001;
public static final int dashboard_home=0x7f030002;
- public static final int image_list=0x7f030003;
- public static final int image_list_item=0x7f030004;
- public static final int login=0x7f030005;
- public static final int take_picture=0x7f030006;
+ public static final int image_grid=0x7f030003;
+ public static final int image_list=0x7f030004;
+ public static final int image_list_item=0x7f030005;
+ public static final int login=0x7f030006;
+ public static final int take_picture=0x7f030007;
}
public static final class menu {
public static final int take_picture_menu=0x7f080000;
View
2  res/layout/dashboard_home.xml
@@ -29,7 +29,7 @@
android:text="@string/feed"
android:drawableTop="@drawable/ic_menu_allfriends"/>
<Button android:id="@+id/popular_button"
- android:onClick="openPopularIntent"
+ android:onClick="openPopularGridIntent"
style="@style/DashboardButton"
android:text="@string/popular"
android:drawableTop="@drawable/ic_menu_star"/>
View
40 res/layout/image_grid.xml
@@ -0,0 +1,40 @@
+<?xml version="1.0" encoding="utf-8"?>
+<!--
+ ~ Copyright 2011, Mark L. Chang <mark.chang@gmail.com>. All rights reserved.
+ ~
+ ~ Redistribution and use in source and binary forms, with or without modification, are
+ ~ permitted provided that the following conditions are met:
+ ~
+ ~ 1. Redistributions of source code must retain the above copyright notice, this list of
+ ~ conditions and the following disclaimer.
+ ~
+ ~ 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ ~ of conditions and the following disclaimer in the documentation and/or other
+ ~ materials provided with the distribution.
+ ~
+ ~ THIS SOFTWARE IS PROVIDED BY Mark L. Chang ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ ~ WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ ~ FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK L. CHANG OR
+ ~ CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ ~ CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ ~ SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ ~ ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ ~ NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ ~ ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ ~
+ ~ The views and conclusions contained in the software and documentation are those of the
+ ~ authors and should not be interpreted as representing official policies, either expressed
+ ~ or implied, of Mark L. Chang.
+ -->
+
+<GridView xmlns:android="http://schemas.android.com/apk/res/android"
+ android:id="@+id/gridview"
+ android:layout_width="fill_parent"
+ android:layout_height="fill_parent"
+ android:numColumns="auto_fit"
+ android:verticalSpacing="0dp"
+ android:horizontalSpacing="0dp"
+ android:columnWidth="75dp"
+ android:stretchMode="columnWidth"
+ android:gravity="center"
+/>
View
7 src/org/acmelab/andgram/DashboardActivity.java
@@ -77,6 +77,13 @@ public void openPopularIntent(View view) {
startActivity(feedIntent);
}
+ public void openPopularGridIntent(View view) {
+ Intent feedIntent = new Intent(DashboardActivity.this, ImageGridActivity.class);
+ feedIntent.putExtra("url", Utils.POPULAR_URL);
+ feedIntent.putExtra("title", R.string.popular);
+ startActivity(feedIntent);
+ }
+
public void openUserfeedIntent(View view) {
Intent feedIntent = new Intent(DashboardActivity.this, ImageListActivity.class);
String pk = Utils.getUserPk(getApplicationContext());
View
268 src/org/acmelab/andgram/ImageGridActivity.java
@@ -0,0 +1,268 @@
+/*
+ * Copyright 2011, Mark L. Chang <mark.chang@gmail.com>. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Mark L. Chang ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK L. CHANG OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of Mark L. Chang.
+ */
+
+package org.acmelab.andgram;
+
+import android.app.Activity;
+import android.app.AlertDialog;
+import android.content.DialogInterface;
+import android.content.Intent;
+import android.os.AsyncTask;
+import android.os.Bundle;
+import android.util.Log;
+import android.view.View;
+import android.widget.*;
+import com.markupartist.android.widget.ActionBar;
+import org.apache.http.HttpEntity;
+import org.apache.http.HttpResponse;
+import org.apache.http.HttpStatus;
+import org.apache.http.NameValuePair;
+import org.apache.http.client.methods.HttpGet;
+import org.apache.http.impl.client.DefaultHttpClient;
+import org.apache.http.message.BasicNameValuePair;
+import org.json.JSONArray;
+import org.json.JSONException;
+import org.json.JSONObject;
+import org.json.JSONTokener;
+
+import java.io.BufferedReader;
+import java.io.InputStreamReader;
+import java.text.SimpleDateFormat;
+import java.util.ArrayList;
+import java.util.Date;
+import java.util.List;
+
+public class ImageGridActivity extends Activity {
+
+ private String sourceUrl;
+
+ GridView grid;
+ LazyGridAdapter adapter;
+ ArrayList<InstagramImage> instagramImageList;
+ DefaultHttpClient httpClient = null;
+
+ @Override
+ public void onCreate(Bundle savedInstanceState) {
+ super.onCreate(savedInstanceState);
+ setContentView(R.layout.image_grid);
+
+ Bundle extras = getIntent().getExtras();
+ sourceUrl = extras.getString("url");
+ grid = (GridView)findViewById(R.id.gridview);
+
+ httpClient = new DefaultHttpClient();
+ httpClient.getParams().setParameter("http.useragent", "Instagram");
+
+ init();
+ }
+
+ private void init() {
+ // set that list to background downloader
+ instagramImageList = new ArrayList<InstagramImage>();
+ adapter = new LazyGridAdapter(this, instagramImageList);
+ grid.setAdapter(adapter);
+ new FetchActivity().execute();
+ }
+
+ private void refresh() {
+ instagramImageList.clear();
+ adapter.notifyDataSetChanged();
+ new FetchActivity().execute();
+ }
+
+
+ @Override
+ public void onDestroy()
+ {
+ adapter.imageLoader.stopThread();
+ grid.setAdapter(null);
+ super.onDestroy();
+ }
+
+ public void clearCache(View view) {
+ adapter.imageLoader.clearCache();
+ adapter.notifyDataSetChanged();
+ }
+
+ private class FetchActivity extends AsyncTask<Void, String, Boolean> {
+ protected void onPreExecute() {
+ }
+
+ protected void onPostExecute(Boolean result) {
+ if(result) {
+ adapter.notifyDataSetChanged();
+ }
+ }
+
+ protected void onProgressUpdate(String toastText) {
+ Toast.makeText(ImageGridActivity.this, toastText, Toast.LENGTH_SHORT).show();
+ }
+
+ protected Boolean doInBackground(Void... voids) {
+ Log.i(Utils.TAG, "Image fetch");
+
+ if( Utils.isOnline(getApplicationContext()) == false ) {
+ publishProgress("No connection to Internet.\nTry again later");
+ Log.i(Utils.TAG, "No internet, didn't load Activity Feed");
+ return false;
+ }
+
+ if( !Utils.doLogin(getApplicationContext(), httpClient) ) {
+ publishProgress("Login failed");
+ return false;
+ } else {
+ try {
+ HttpGet httpGet = new HttpGet(sourceUrl);
+ HttpResponse httpResponse = httpClient.execute(httpGet);
+
+ // test result code
+ if( httpResponse.getStatusLine().getStatusCode() != HttpStatus.SC_OK ) {
+ publishProgress("Login failed.");
+ Log.e(Utils.TAG, "Login status code bad.");
+ return false;
+ }
+
+ // test json response
+ HttpEntity httpEntity = httpResponse.getEntity();
+ if( httpEntity != null ) {
+ BufferedReader reader = new BufferedReader(new InputStreamReader(httpEntity.getContent(), "UTF-8"));
+ String json = reader.readLine();
+ JSONTokener jsonTokener = new JSONTokener(json);
+ JSONObject jsonObject = new JSONObject(jsonTokener);
+ Log.i(Utils.TAG,"JSON: " + jsonObject.toString());
+
+ String loginStatus = jsonObject.getString("status");
+
+ if( !loginStatus.equals("ok") ) {
+ publishProgress("Activity feed did not return status ok");
+ Log.e(Utils.TAG, "JSON status not ok: " + jsonObject.getString("status"));
+ return false;
+ } else {
+ // parse the activity feed
+ Log.i(Utils.TAG, "Getting images from activity feed");
+ JSONArray items = jsonObject.getJSONArray("items");
+
+ // get image URLs and commentary
+ for( int i=0; i< items.length(); i++ ) {
+ // create a new instance
+ InstagramImage instagramImage = new InstagramImage();
+
+ // image
+ JSONObject entry = (JSONObject)items.get(i);
+ JSONArray imageVersions = entry.getJSONArray("image_versions");
+ JSONObject bigImage = (JSONObject)imageVersions.get(2);
+ instagramImage.url = bigImage.getString("url");
+ instagramImage.pk = ((Long)entry.getLong("pk")).toString();
+
+ // user
+ JSONObject user = entry.getJSONObject("user");
+ instagramImage.username = user.getString("username");
+
+ // date taken_at
+ Long dateLong = entry.getLong("taken_at");
+ SimpleDateFormat formatter = new SimpleDateFormat("MMMM d, yyyy HH:mm");
+ instagramImage.taken_at = formatter.format(new Date(dateLong * 1000L));
+
+ // comments (and caption)
+ JSONArray comments = entry.getJSONArray("comments");
+ if( comments != null ) {
+ ArrayList<Comment> commentList = new ArrayList<Comment>();
+ for( int c=0; c < comments.length(); c++ ) {
+ JSONObject comment = comments.getJSONObject(c);
+ user = comment.getJSONObject("user");
+ if(c==0) {
+ instagramImage.caption = comment.getString("text");
+ } else {
+ commentList.add(new Comment(user.getString("username"),
+ comment.getString("text")));
+ }
+ }
+ instagramImage.comment_list = commentList;
+ }
+
+ // likers
+ try {
+ JSONArray liker_ids = entry.getJSONArray("liker_ids");
+ if( liker_ids != null) {
+ if( liker_ids.length() > 0 ) {
+ ArrayList<String> likerList = new ArrayList<String>();
+ likerList.add(Integer.toString(liker_ids.length()));
+ instagramImage.liker_list = likerList;
+ instagramImage.liker_list_is_count = true;
+ }
+ }
+ } catch( JSONException j ) {}
+
+ try {
+ JSONArray likers = entry.getJSONArray("likers");
+ if( likers != null ) {
+ ArrayList<String> likerList = new ArrayList<String>();
+ StringBuilder likerString = new StringBuilder();
+ if( likers.length() > 0 ) {
+ likerString.append("Liked by: <b>");
+ for( int l=0; l < likers.length(); l++ ) {
+ JSONObject like = likers.getJSONObject(l);
+ likerString.append(like.getString("username") + " ");
+ likerList.add(like.getString("username"));
+ }
+ likerString.append("</b>");
+ instagramImage.liker_list = likerList;
+ instagramImage.liker_list_is_count = false;
+ }
+ }
+ } catch( JSONException j ) {}
+
+ instagramImageList.add(instagramImage);
+ }
+
+ return true;
+ }
+ } else {
+ publishProgress("Improper data returned from Instagram");
+ Log.e(Utils.TAG, "instagram returned bad data");
+ return false;
+ }
+ } catch (Exception e) {
+ e.printStackTrace();
+ return false;
+ }
+ }
+ }
+ }
+
+ private class RefreshAction implements ActionBar.Action {
+
+ public int getDrawable() {
+ return R.drawable.ic_title_refresh;
+ }
+
+ public void performAction(View view) {
+ refresh();
+ }
+ }
+}
View
102 src/org/acmelab/andgram/ImageLoader.java
@@ -77,16 +77,18 @@ public ImageLoader(Context context){
public void DisplayImage(String url, Activity activity, ImageView imageView)
{
- Log.i(Utils.TAG, "Display: " + url);
+ Log.i(Utils.TAG, "Displaying image: " + url);
if(cache.containsKey(url)) {
+ Log.i(Utils.TAG, "Found cached image " + url);
SoftReference<Bitmap> softRef = cache.get(url);
Bitmap bitmap = softRef.get();
if( bitmap == null ) {
- Log.i(Utils.TAG, "Re-queuing GC'ed image: " + url);
+ Log.i(Utils.TAG, "But re-queuing GC'ed image: " + url);
// maybe? : cache.remove(softRef);
queuePhoto(url, activity, imageView);
imageView.setImageResource(stub_id);
} else {
+ Log.i(Utils.TAG, "Setting image bitmap");
imageView.setImageBitmap(softRef.get());
if( softRef.get() == null ) {
Log.e(Utils.TAG, "Null bitmap: " + url);
@@ -95,6 +97,7 @@ public void DisplayImage(String url, Activity activity, ImageView imageView)
}
else
{
+ Log.i(Utils.TAG, "Not in cache, queueing " + url);
queuePhoto(url, activity, imageView);
imageView.setImageResource(stub_id);
}
@@ -104,10 +107,11 @@ private void queuePhoto(String url, Activity activity, ImageView imageView)
{
Log.i(Utils.TAG, "Queueing: " + url);
- //This ImageView may be used for other images before. So there may be some old tasks in the queue. We need to discard them.
+ // This ImageView may be used for other images before.
+ // So there may be some old tasks in the queue. We need to discard them.
photosQueue.Clean(imageView);
PhotoToLoad p=new PhotoToLoad(url, imageView);
- synchronized(photosQueue.photosToLoad){
+ synchronized( photosQueue.photosToLoad ){
photosQueue.photosToLoad.push(p);
photosQueue.photosToLoad.notifyAll();
}
@@ -120,21 +124,27 @@ private void queuePhoto(String url, Activity activity, ImageView imageView)
private Bitmap getBitmap(String url)
{
// I identify images by hashcode. Not a perfect solution, good for the demo.
+ Log.i(Utils.TAG, "Getting image in thread: " + url);
String filename=String.valueOf(url.hashCode());
File f=new File(cacheDir, filename);
//from SD cache
Bitmap b = decodeFile(f);
- if(b!=null)
+ if(b != null) {
+ Log.i(Utils.TAG, "Found in cache." );
return b;
+ }
+
//from web
try {
- Bitmap bitmap=null;
- InputStream is=new URL(url).openStream();
+ Log.i(Utils.TAG, "Downloading from web");
+ Bitmap bitmap = null;
+ InputStream is = new URL(url).openStream();
OutputStream os = new FileOutputStream(f);
Utils.CopyStream(is, os);
os.close();
+ is.close();
bitmap = decodeFile(f);
return bitmap;
} catch (Exception ex){
@@ -146,30 +156,18 @@ private Bitmap getBitmap(String url)
//decodes image and scales it to reduce memory consumption
private Bitmap decodeFile(File f){
try {
- //decode image size
- // BitmapFactory.Options o = new BitmapFactory.Options();
- // o.inJustDecodeBounds = true;
- return(BitmapFactory.decodeStream(new FileInputStream(f),null,null));
- /*
- //Find the correct scale value. It should be the power of 2.
- final int REQUIRED_SIZE=70;
- int width_tmp=o.outWidth, height_tmp=o.outHeight;
- int scale=1;
- while(true){
- if(width_tmp/2<REQUIRED_SIZE || height_tmp/2<REQUIRED_SIZE)
- break;
- width_tmp/=2;
- height_tmp/=2;
- scale*=2;
- }
-
- //decode with inSampleSize
- BitmapFactory.Options o2 = new BitmapFactory.Options();
- o2.inSampleSize=scale;
- return BitmapFactory.decodeStream(new FileInputStream(f), null, o2);
- */
- } catch (FileNotFoundException e) {}
- return null;
+ Log.i(Utils.TAG, "Decoding image: " + f.toString());
+ Bitmap b = BitmapFactory.decodeStream(new FileInputStream(f));
+ Log.i(Utils.TAG, "decoded a real bitmap!");
+ return(b);
+ } catch (FileNotFoundException e) {
+ Log.i(Utils.TAG, "File not found");
+ return null;
+ } catch (Exception ex) {
+ Log.e(Utils.TAG, "Some other shit failed in decodeFile");
+ ex.printStackTrace();
+ return null;
+ }
}
// Task for the queue
@@ -178,12 +176,12 @@ private Bitmap decodeFile(File f){
public String url;
public ImageView imageView;
public PhotoToLoad(String u, ImageView i){
- url=u;
- imageView=i;
+ url = u;
+ imageView = i;
}
}
- PhotosQueue photosQueue=new PhotosQueue();
+ PhotosQueue photosQueue = new PhotosQueue();
public void stopThread()
{
@@ -199,7 +197,7 @@ public void stopThread()
public void Clean(ImageView image)
{
for(int j=0 ;j<photosToLoad.size();){
- if(photosToLoad.get(j).imageView==image)
+ if(photosToLoad.get(j).imageView == image)
photosToLoad.remove(j);
else
++j;
@@ -213,23 +211,30 @@ public void run() {
while(true)
{
//thread waits until there are any images to load in the queue
- if(photosQueue.photosToLoad.size()==0)
+ if(photosQueue.photosToLoad.size() == 0) {
synchronized(photosQueue.photosToLoad){
photosQueue.photosToLoad.wait();
}
- if(photosQueue.photosToLoad.size()!=0)
+ }
+ if(photosQueue.photosToLoad.size() != 0)
{
PhotoToLoad photoToLoad;
synchronized(photosQueue.photosToLoad){
photoToLoad=photosQueue.photosToLoad.pop();
}
- Bitmap bmp=getBitmap(photoToLoad.url);
- cache.put(photoToLoad.url, new SoftReference<Bitmap>(bmp));
- Object tag=photoToLoad.imageView.getTag();
- if(tag!=null && ((String)tag).equals(photoToLoad.url)){
- BitmapDisplayer bd=new BitmapDisplayer(bmp, photoToLoad.imageView);
- Activity a=(Activity)photoToLoad.imageView.getContext();
- a.runOnUiThread(bd);
+ Bitmap bmp = getBitmap(photoToLoad.url);
+ if(bmp == null) {
+ Log.e(Utils.TAG, "Bitmap loaded as null!");
+ } else {
+ cache.put(photoToLoad.url, new SoftReference<Bitmap>(bmp));
+ Object tag = photoToLoad.imageView.getTag();
+ if(tag != null && ((String)tag).equals(photoToLoad.url)){
+ BitmapDisplayer bd = new BitmapDisplayer(bmp, photoToLoad.imageView);
+ Activity a = (Activity)photoToLoad.imageView.getContext();
+ a.runOnUiThread(bd);
+ } else {
+ Log.i(Utils.TAG, "Got image w/o tag!");
+ }
}
}
if(Thread.interrupted())
@@ -248,14 +253,15 @@ public void run() {
{
Bitmap bitmap;
ImageView imageView;
- public BitmapDisplayer(Bitmap b, ImageView i){
- bitmap=b;
- imageView=i;
+
+ public BitmapDisplayer(Bitmap b, ImageView i) {
+ bitmap = b;
+ imageView = i;
}
public void run()
{
- if(bitmap!=null)
+ if(bitmap != null)
imageView.setImageBitmap(bitmap);
else
imageView.setImageResource(stub_id);
View
90 src/org/acmelab/andgram/LazyGridAdapter.java
@@ -0,0 +1,90 @@
+/*
+ * Copyright 2011, Mark L. Chang <mark.chang@gmail.com>. All rights reserved.
+ *
+ * Redistribution and use in source and binary forms, with or without modification, are
+ * permitted provided that the following conditions are met:
+ *
+ * 1. Redistributions of source code must retain the above copyright notice, this list of
+ * conditions and the following disclaimer.
+ *
+ * 2. Redistributions in binary form must reproduce the above copyright notice, this list
+ * of conditions and the following disclaimer in the documentation and/or other
+ * materials provided with the distribution.
+ *
+ * THIS SOFTWARE IS PROVIDED BY Mark L. Chang ``AS IS'' AND ANY EXPRESS OR IMPLIED
+ * WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
+ * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL MARK L. CHANG OR
+ * CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
+ * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
+ * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON
+ * ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
+ * NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF
+ * ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
+ *
+ * The views and conclusions contained in the software and documentation are those of the
+ * authors and should not be interpreted as representing official policies, either expressed
+ * or implied, of Mark L. Chang.
+ */
+
+package org.acmelab.andgram;
+
+import android.app.Activity;
+import android.content.Context;
+import android.text.Html;
+import android.util.Log;
+import android.view.LayoutInflater;
+import android.view.View;
+import android.view.ViewGroup;
+import android.widget.BaseAdapter;
+import android.widget.GridView;
+import android.widget.ImageView;
+import android.widget.TextView;
+
+import java.util.ArrayList;
+
+public class LazyGridAdapter extends BaseAdapter {
+
+ private Activity activity;
+ private ArrayList<InstagramImage> instagramImageArrayList;
+ private static LayoutInflater inflater = null;
+ public ImageLoader imageLoader;
+
+ public LazyGridAdapter(Activity a, ArrayList<InstagramImage> i) {
+ activity = a;
+ instagramImageArrayList = i;
+ inflater = (LayoutInflater)activity.getSystemService(Context.LAYOUT_INFLATER_SERVICE);
+ imageLoader = new ImageLoader(activity.getApplicationContext());
+ }
+
+ public int getCount() {
+ return instagramImageArrayList.size();
+ }
+
+ public Object getItem(int position) {
+ return instagramImageArrayList.get(position);
+ }
+
+ public long getItemId(int position) {
+ return position;
+ }
+
+ public View getView(int position, View convertView, ViewGroup parent) {
+ Log.i(Utils.TAG, "Getting grid view " + Integer.toString(position));
+ ImageView imageView;
+ if (convertView == null) { // if it's not recycled, initialize some attributes
+ Log.i(Utils.TAG, "Creating view for the first time");
+ imageView = new ImageView(activity);
+ imageView.setLayoutParams(new GridView.LayoutParams(75, 75));
+ imageView.setScaleType(ImageView.ScaleType.CENTER_CROP);
+ imageView.setPadding(0, 0, 0, 0);
+ } else {
+ Log.i(Utils.TAG, "Got a view");
+ imageView = (ImageView) convertView;
+ }
+
+ InstagramImage image = instagramImageArrayList.get(position);
+ imageView.setTag(image.url);
+ imageLoader.DisplayImage(image.url, activity, imageView);
+ return imageView;
+ }
+}
Please sign in to comment.
Something went wrong with that request. Please try again.