All Downloads are FREE. Search and download functionalities are using the official Maven repository.

it.tidalwave.bluebill.mobile.android.taxonomy.factsheet.AndroidStaticImageRenderer Maven / Gradle / Ivy

/***********************************************************************************************************************
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet;

import android.content.Context;
import android.graphics.Bitmap;
import android.net.Uri;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.Node;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.StaticImageNode;
import it.tidalwave.mobile.android.ui.AndroidUtilities;
import it.tidalwave.mobile.media.Media;
import it.tidalwave.mobile.util.Downloadable;
import it.tidalwave.util.Id;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.logging.Logger;
import java.io.File;
import java.util.HashMap;
import java.util.Map;
import javax.annotation.Nonnull;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public class AndroidStaticImageRenderer implements AndroidNodeRenderer
  {
    private static final String CLASS = AndroidStaticImageRenderer.class.getName();
    private static final Logger logger = Logger.getLogger(CLASS);

    private final Map map = new HashMap(); // FIXME: make weak!

    private class DownloaderThread extends Thread
      {
        @Nonnull
        private final StaticImageNode node;

        @Nonnull
        private final ImageView imageView;

        public DownloaderThread (final @Nonnull StaticImageNode node, final @Nonnull ImageView imageView)
          {
            this.node = node;
            this.imageView = imageView;
          }

        @Override
        public void run()
          {
            try
              {
                final Media media = new Media().with(Media.ID, new Id(node.getUri().toASCIIString()));
                final Downloadable downloadable = media.as(Downloadable.class);
                downloadable.download();
                downloadable.waitUntilDownloadingCompleted();
                final File file = downloadable.getFile();
                logger.info("Downloading image to cache: %s", file.getAbsolutePath());
                final Bitmap image = AndroidUtilities.loadBitmap(Uri.fromFile(file).toString());
                map.put(node, image);
                imageView.post(new Runnable()
                  {
                    public void run()
                      {
                        logger.info(">>>> calling setImageDrawable(%s)", image);
                        imageView.setImageBitmap(image);
                      }
                  });
              }
            catch (InterruptedException e)
              {
                logger.severe("Could not load image: %s", e.getMessage());
              }
            catch (NotFoundException e)
              {
                logger.severe("Could not load image: %s", e.getMessage());
              }
            catch (NullPointerException e) // defensive, see: BBMA-67
              {
                logger.severe("Could not load image: %s", e.getMessage());
              }
          }
      }

    @Nonnull
    private final Context context;

    @Nonnull
    private final LayoutInflater layoutInflater;

    private final int layoutId;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AndroidStaticImageRenderer (final @Nonnull Context context)
      {
        this(context, R.layout.image_list_row);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public AndroidStaticImageRenderer (final @Nonnull Context context, final int layoutId)
      {
        this.context = context;
        this.layoutId = layoutId;
        layoutInflater = LayoutInflater.from(context);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public View getView (final @Nonnull StaticImageNode node, final @Nonnull ViewGroup viewGroup)
      throws Exception
      {
        final View view = layoutInflater.inflate(layoutId, null);
        final ImageView ivImageView = (ImageView)view.findViewById(R.id.ivImageView);
        final Bitmap image = map.get(node);

        if (image != null)
          {
            ivImageView.setImageBitmap(image);
          }
        else
          {
            new DownloaderThread(node, ivImageView).start();
          }

        return view;
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy