Android Open Source - GalDroid Image Adapter






From Project

Back to project page GalDroid.

License

The source code is released under:

GNU General Public License

If you think the Android project GalDroid listed in this page is inappropriate, such as containing malicious code/tools or violating the copyright, please email info at java2s dot com, thanks.

Java Source Code

/*
 * GalDroid - a webgallery frontend for android
 * Copyright (C) 2011  Raptor 2101 [raptor2101@gmx.de]
 *    //ww w. j  av  a2 s . com
 * This program is free software: you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation, either version 3 of the License, or
 * (at your option) any later version.

 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.

 * You should have received a copy of the GNU General Public License
 * along with this program.  If not, see <http://www.gnu.org/licenses/>.  
 */

package de.raptor2101.GalDroid.Activities.Helpers;

import java.lang.ref.WeakReference;
import java.util.ArrayList;
import java.util.List;

import android.content.Context;
import android.graphics.Bitmap;
import android.util.Log;
import android.view.View;
import android.view.ViewGroup;
import android.view.ViewGroup.LayoutParams;
import android.widget.BaseAdapter;
import de.raptor2101.GalDroid.Activities.GalDroidApp;
import de.raptor2101.GalDroid.Activities.Views.GalleryImageView;
import de.raptor2101.GalDroid.Activities.Views.GalleryImageViewListener;
import de.raptor2101.GalDroid.WebGallery.ImageCache;
import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject;
import de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObject;
import de.raptor2101.GalDroid.WebGallery.Interfaces.WebGallery.ImageSize;
import de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTask;
import de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTask.ImageDownload;

public class ImageAdapter extends BaseAdapter {
  private final static String ClassTag = "GalleryImageAdapter";

  public enum DisplayTarget {
    Thumbnails, FullScreen
  }

  public enum TitleConfig {
    ShowTitle, HideTitle
  }

  public enum ScaleMode {
    ScaleSource, DontScale
  }

  // private WebGallery mWebGallery;
  private ImageCache mCache;
  private Context mContext;

  private List<GalleryObject> mGalleryObjects;
  private TitleConfig mTitleConfig;
  private LayoutParams mLayoutParams;
  private ScaleMode mScaleMode;
  private ImageSize mImageSize;

  private WeakReference<GalleryImageViewListener> mListener;
  private ArrayList<WeakReference<GalleryImageView>> mImageViews;

  private ImageLoaderTask mImageLoaderTask;

  public ImageAdapter(Context context, LayoutParams layoutParams, ScaleMode scaleMode, ImageLoaderTask loaderTask) {
    super();
    GalDroidApp appContext = (GalDroidApp) context.getApplicationContext();
    mContext = context;
    mCache = appContext.getImageCache();

    mGalleryObjects = new ArrayList<GalleryObject>(0);
    mImageViews = new ArrayList<WeakReference<GalleryImageView>>(0);

    mTitleConfig = TitleConfig.ShowTitle;
    mImageSize = ImageSize.Thumbnail;
    mScaleMode = scaleMode;
    mLayoutParams = layoutParams;

    mListener = new WeakReference<GalleryImageViewListener>(null);
    mImageLoaderTask = loaderTask;
  }

  public void setListener(GalleryImageViewListener listener) {
    mListener = new WeakReference<GalleryImageViewListener>(listener);
  }

  public void setGalleryObjects(List<GalleryObject> galleryObjects) {
    if (isLoaded()) {
      cleanUp();
    }
    this.mGalleryObjects = galleryObjects;
    this.mImageViews = new ArrayList<WeakReference<GalleryImageView>>(galleryObjects.size());

    for (int i = 0; i < galleryObjects.size(); i++) {
      mImageViews.add(new WeakReference<GalleryImageView>(null));
    }

    notifyDataSetChanged();
  }

  public void setTitleConfig(TitleConfig titleConfig) {
    this.mTitleConfig = titleConfig;
  }

  public void setDisplayTarget(DisplayTarget displayTarget) {
    mImageSize = displayTarget == DisplayTarget.FullScreen ? ImageSize.Full : ImageSize.Thumbnail;
  }

  public List<GalleryObject> getGalleryObjects() {
    return mGalleryObjects;
  }

  public int getCount() {

    return mGalleryObjects.size();
  }

  public Object getItem(int position) {
    return mGalleryObjects.get(position);
  }

  public long getItemId(int position) {
    return position;
  }

  public View getView(int position, View cachedView, ViewGroup parent) {
    GalleryObject galleryObject = mGalleryObjects.get(position);

    String objectId = galleryObject.getObjectId();
    Log.d(ClassTag, String.format("Request Pos %s View %s", position, objectId));

    GalleryImageView imageView = getCachedView(cachedView, galleryObject);

    imageView = mImageViews.get(position).get();
    if (imageView == null) {
      Log.d(ClassTag, String.format("Miss ImageView Reference", objectId));
      imageView = CreateImageView(galleryObject);
      mImageViews.set(position, new WeakReference<GalleryImageView>(imageView));
    }

    GalleryDownloadObject downloadObject = getDownloadObject(galleryObject);
    boolean isLoading = imageView.isLoading();
    Log.d(ClassTag, String.format("%s isLoaded: %s", objectId, isLoading));
    if (!isLoading) {
      Log.d(ClassTag, String.format("Init Reload", galleryObject.getObjectId()));
      loadGalleryImage(imageView, downloadObject);
    }

    return imageView;
  }

  private GalleryDownloadObject getDownloadObject(GalleryObject galleryObject) {
    GalleryDownloadObject downloadObject = mImageSize == ImageSize.Full ? galleryObject.getImage() : galleryObject.getThumbnail();
    return downloadObject;
  }

  private GalleryImageView CreateImageView(GalleryObject galleryObject) {
    GalleryImageView imageView;
    imageView = new GalleryImageView(mContext, this.mTitleConfig == TitleConfig.ShowTitle);
    imageView.setLayoutParams(mLayoutParams);
    imageView.setGalleryObject(galleryObject);
    imageView.setListener(mListener.get());
    return imageView;
  }

  private GalleryImageView getCachedView(View cachedView, GalleryObject galleryObject) {
    GalleryImageView imageView = null;
    if (cachedView != null) {
      imageView = (GalleryImageView) cachedView;
      GalleryObject originGalleryObject = imageView.getGalleryObject();
      boolean equalsObject = originGalleryObject.getObjectId().equals(galleryObject.getObjectId());
      boolean isLoaded = imageView.isLoaded();
      Log.d(ClassTag, String.format("Cached View %s, GalleryObjectEquals %s, isLoaded %s", imageView.getObjectId(), equalsObject, isLoaded));
      if (!equalsObject) {
        if (!isLoaded) {
          Log.d(ClassTag, String.format("Abort downloadTask %s", imageView.getObjectId()));
          try {
            mImageLoaderTask.cancel(getDownloadObject(originGalleryObject), false);
          } catch (InterruptedException e) {
            // nothing to do here...
          }
        }
      }
    }
    return imageView;
  }

  private void loadGalleryImage(GalleryImageView imageView, GalleryDownloadObject downloadObject) {
    if (downloadObject == null) {
      imageView.resetLoading();
      return;
    }

    Bitmap cachedBitmap = mCache.getBitmap(downloadObject.getUniqueId());
    if (cachedBitmap == null) {
      ImageDownload imageDownload = imageView.getImageDownload();
      if(imageDownload != null) {
        imageDownload.updateListener(null);
      }
      LayoutParams layoutParams = mScaleMode == ScaleMode.ScaleSource ? mLayoutParams : null;
      imageDownload = mImageLoaderTask.download(downloadObject, layoutParams, imageView);
      imageView.setImageDownload(imageDownload);
      
    } else {
      imageView.onLoadingCompleted(downloadObject.getUniqueId(), cachedBitmap);
    }
  }

  public void cleanUp() {
    Log.d(ClassTag, String.format("CleanUp"));
    for (WeakReference<GalleryImageView> reference : mImageViews) {
      GalleryImageView imageView = reference.get();
      if (imageView != null) {
        Log.d(ClassTag, String.format("CleanUp ", imageView.getObjectId()));
        imageView.cleanup();
      }
    }
    System.gc();
  }

  public void refreshImages() {
    for (WeakReference<GalleryImageView> reference : mImageViews) {
      GalleryImageView imageView = reference.get();
      if (imageView != null && !imageView.isLoaded()) {
        GalleryObject galleryObject = imageView.getGalleryObject();
        Log.d(ClassTag, String.format("Relaod ", imageView.getObjectId()));
        GalleryDownloadObject downloadObject = getDownloadObject(galleryObject);
        loadGalleryImage(imageView, downloadObject);
      }
    }
  }

  public boolean isLoaded() {
    return mGalleryObjects.size() != 0;
  }

  public ImageLoaderTask getImageLoaderTask() {
    return mImageLoaderTask;
  }
}




Java Source Code List

de.raptor2101.GalDroid.Activities.EditGalleryActivity.java
de.raptor2101.GalDroid.Activities.GalDroidApp.java
de.raptor2101.GalDroid.Activities.GalleryActivity.java
de.raptor2101.GalDroid.Activities.GalleryListingActivitiy.java
de.raptor2101.GalDroid.Activities.GridViewActivity.java
de.raptor2101.GalDroid.Activities.ImageViewActivity.java
de.raptor2101.GalDroid.Activities.Helpers.ActionBarHider.java
de.raptor2101.GalDroid.Activities.Helpers.ImageAdapter.java
de.raptor2101.GalDroid.Activities.Listeners.ImageViewOnTouchListener.java
de.raptor2101.GalDroid.Activities.Views.GalleryImageViewListener.java
de.raptor2101.GalDroid.Activities.Views.GalleryImageView.java
de.raptor2101.GalDroid.Activities.Views.ImageInformationView.java
de.raptor2101.GalDroid.Config.GalDroidPreference.java
de.raptor2101.GalDroid.Config.GalleryConfig.java
de.raptor2101.GalDroid.WebGallery.DegMinSec.java
de.raptor2101.GalDroid.WebGallery.GalleryFactory.java
de.raptor2101.GalDroid.WebGallery.ImageCache.java
de.raptor2101.GalDroid.WebGallery.ImageInformation.java
de.raptor2101.GalDroid.WebGallery.Stream.java
de.raptor2101.GalDroid.WebGallery.TitleConfig.java
de.raptor2101.GalDroid.WebGallery.Gallery3.DownloadObject.java
de.raptor2101.GalDroid.WebGallery.Gallery3.Gallery3Imp.java
de.raptor2101.GalDroid.WebGallery.Gallery3.ProgressListener.java
de.raptor2101.GalDroid.WebGallery.Gallery3.RestCall.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.AlbumEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.CommentEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.EntityFactory.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.Entity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.JSON.PictureEntity.java
de.raptor2101.GalDroid.WebGallery.Gallery3.Tasks.JSONArrayLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryDownloadObject.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObjectComment.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryObject.java
de.raptor2101.GalDroid.WebGallery.Interfaces.GalleryProgressListener.java
de.raptor2101.GalDroid.WebGallery.Interfaces.WebGallery.java
de.raptor2101.GalDroid.WebGallery.Tasks.CacheTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.CleanUpCacheTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.GalleryVerifyTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageInformationLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageInformationLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTaskListener.java
de.raptor2101.GalDroid.WebGallery.Tasks.ImageLoaderTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.Progress.java
de.raptor2101.GalDroid.WebGallery.Tasks.RepeatingTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.SyncronizeCacheTask.java
de.raptor2101.GalDroid.WebGallery.Tasks.TaskInterface.java