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

it.tidalwave.ui.android.widget.StillImageViewer Maven / Gradle / Ivy

There is a newer version: 1.0.13
Show newest version
/***********************************************************************************************************************
 *
 * blueBill Mobile - Android - open source birding
 * Copyright (C) 2009-2011 by Tidalwave s.a.s. (http://www.tidalwave.it)
 *
 ***********************************************************************************************************************
 *
 * Licensed under the Apache License, Version 2.0 (the "License"); you may not use this file except in compliance with
 * the License. You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software distributed under the License is distributed on
 * an "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.  See the License for the
 * specific language governing permissions and limitations under the License.
 *
 ***********************************************************************************************************************
 *
 * WWW: http://bluebill.tidalwave.it/mobile
 * SCM: https://java.net/hg/bluebill-mobile~android-src
 *
 **********************************************************************************************************************/
package it.tidalwave.ui.android.widget;

import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.beans.PropertyChangeEvent;
import java.beans.PropertyChangeListener;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import it.tidalwave.role.ui.PresentationModel;
import it.tidalwave.mobile.util.Downloadable;
import android.util.AttributeSet;
import android.content.Context;
import android.net.Uri;
import android.widget.ImageSwitcher;
import it.tidalwave.mobile.android.ui.AndroidUtilities;
import static it.tidalwave.role.Identifiable.Identifiable;
import static it.tidalwave.mobile.util.Downloadable.*;
import static it.tidalwave.mobile.util.Downloadable.Status.*;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id$
 *
 **********************************************************************************************************************/
public class StillImageViewer extends ImageSwitcher
  {
    private static final Logger log = LoggerFactory.getLogger(StillImageViewer.class);

    @CheckForNull
    private PresentationModel image;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public StillImageViewer (final @Nonnull Context context, final @Nonnull AttributeSet attrs)
      {
        super(context, attrs);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public StillImageViewer (final @Nonnull Context context)
      {
        super(context);
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final PropertyChangeListener imageStatusListener = new PropertyChangeListener() 
      {
        public void propertyChange (final @Nonnull PropertyChangeEvent event) 
          {
            if (PROP_STATUS.equals(event.getPropertyName())) // FIXME: drop this, use a property-specific listener
              {
                final Status status = (Status)event.getNewValue();
                log.info("download change status for {}: {}", image.as(Identifiable).getId(), status);

                if (status.isFinal())
                  {
                    image.removePropertyChangeListener(downloadProgressListener); 
                  }
                
                switch (status)
                  {
                    case DOWNLOADED:
                        doRender();
                        break;

                    case BROKEN:
                        log.warn("broken image: {}", image.as(Identifiable).getId());
                        // FIXME: show broken
                  } 
              }
          }
      };
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private final PropertyChangeListener downloadProgressListener = new PropertyChangeListener() 
      {
        public void propertyChange (final @Nonnull PropertyChangeEvent event) 
          {
            if (PROP_DOWNLOAD_PROGRESS.equals(event.getPropertyName())) // FIXME: drop this, use a property-specific listener
              {
                final float progress = (Float)event.getNewValue();
                log.debug("download progress for {}: {}", image.as(Identifiable).getId(), progress);
              }
          }
      };
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    public void setImage (final @Nonnull PresentationModel image)
      {
        if (this.image != null)
          {
            this.image.as(Downloadable).removePropertyChangeListener(imageStatusListener);
          }

        this.image = image;
        final Downloadable downloadable = image.as(Downloadable);
        downloadable.addPropertyChangeListener(imageStatusListener); 
//        downloadable.addPropertyChangeListener(PROP_STATUS, imageStatusListener); FIXME

        // FIXME: the rule must be moved to the controller (so it can check network permissions etc...)
        if (downloadable.getStatus() == DOWNLOADED)
          {
            log.debug(">>>> image is ready, rendering it...");
            doRender();
          }

        else
          {
            log.debug(">>>> image is not ready, downloading it...");
//            image.download();  

            if (downloadable.getStatus() != DOWNLOADED)
              {
                downloadable.addPropertyChangeListener(downloadProgressListener);
//                downloadable.addPropertyChangeListener(PROP_DOWNLOAD_PROGRESS, downloadProgressListener); FIXME
                downloadable.download();
              }
            
            AndroidUtilities.runOnUIThread(new Runnable()
              {
                public void run() 
                  {
                    setImageResource(android.R.drawable.gallery_thumb);
                  }
              });
          }
      }
    
    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    private void doRender()
      {
        final Uri uri = Uri.parse(image.as(Downloadable).getFile().getAbsolutePath()); // Uri.fromFile(file) doesn't work here
        log.debug(">>>> loading image from {}...", uri);

        AndroidUtilities.runOnUIThread(new Runnable()
          {
            public void run() 
              {
                try
                  {
                    setImageURI(uri); // FIXME: is this blocking?
                  }
                catch (OutOfMemoryError e)
                  {
                    log.error("Cannot display image because OutOfMemoryError: {}", uri); // FIXME: user notification
                  }
              }
          });
      }
  }




© 2015 - 2025 Weber Informatics LLC | Privacy Policy