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

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

/***********************************************************************************************************************
 *
 * blueBill Mobile - open source birdwatching
 * ==========================================
 *
 * Copyright (C) 2009, 2010 by Tidalwave s.a.s. (http://www.tidalwave.it)
 * http://bluebill.tidalwave.it/mobile/
 *
 ***********************************************************************************************************************
 *
 * 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.
 *
 ***********************************************************************************************************************
 *
 * $Id: AndroidMediaNodeRenderer.java,v de87bb2f8007 2010/07/16 16:24:32 fabrizio $
 *
 **********************************************************************************************************************/
package it.tidalwave.bluebill.mobile.android.taxonomy.factsheet;

import android.content.Context;
import android.view.LayoutInflater;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.MediaNode;
import javax.annotation.CheckForNull;
import javax.annotation.Nonnull;
import java.util.HashMap;
import java.util.Map;
import java.util.MissingResourceException;
import org.openide.util.NbBundle;
import it.tidalwave.util.NotFoundException;
import it.tidalwave.util.Key;
import it.tidalwave.mobile.util.Downloadable.Status;
import it.tidalwave.mobile.util.Downloadable;
import it.tidalwave.mobile.media.Media;
import it.tidalwave.semantic.GenericEntity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;
import it.tidalwave.bluebill.factsheet.bbc.FactSheet;
import it.tidalwave.bluebill.mobile.android.R;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.TaxonFactSheetController.DefaultAction;
import it.tidalwave.bluebill.mobile.taxonomy.factsheet.sound.TaxonSoundFactSheetController.DefaultSoundAction;
import it.tidalwave.mobile.android.ui.AndroidUtilities;
import it.tidalwave.semantic.EntityWithProperties;
import javax.annotation.Nonnegative;

/***********************************************************************************************************************
 *
 * @author  Fabrizio Giudici
 * @version $Id: $
 *
 **********************************************************************************************************************/
public abstract class AndroidMediaNodeRenderer implements AndroidNodeRenderer
  {
    protected final static Map COLOR_MAP_BY_STATUS = new HashMap();

    protected final static Map, Integer> ICON_MAP_BY_ACTION = new HashMap, Integer>();
    
    protected final static GenericEntity UNKNOWN_AUTHOR = GenericEntity.namedEntity("");
    protected final static GenericEntity UNKNOWN_LOCATION = GenericEntity.namedEntity("");

    @Nonnull
    private final Context context;

    @Nonnull
    private final LayoutInflater layoutInflater;

    private final int layoutId;

    @Nonnull
    protected View view;
    
    @CheckForNull
    protected Downloadable downloadable;

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    static
      {
        COLOR_MAP_BY_STATUS.put(Status.BROKEN,         R.color.download_status_broken);
        COLOR_MAP_BY_STATUS.put(Status.DOWNLOADED,     R.color.download_status_downloaded);
        COLOR_MAP_BY_STATUS.put(Status.DOWNLOADING,    R.color.download_status_downloading);
        COLOR_MAP_BY_STATUS.put(Status.NOT_DOWNLOADED, R.color.download_status_not_downloaded);
        COLOR_MAP_BY_STATUS.put(Status.OBSOLETE,       R.color.download_status_obsolete);
        COLOR_MAP_BY_STATUS.put(Status.QUEUED,         R.color.download_status_queued);

        ICON_MAP_BY_ACTION.put(DefaultSoundAction.DOWNLOAD, R.drawable.download);
        ICON_MAP_BY_ACTION.put(DefaultSoundAction.PLAY,     android.R.drawable.ic_media_play);
        ICON_MAP_BY_ACTION.put(DefaultSoundAction.STOP,     android.R.drawable.ic_media_pause);
      }

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

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    public View getView (final @Nonnull T node, final @Nonnull ViewGroup viewGroup)
      throws Exception
      {
        view = layoutInflater.inflate(layoutId, null);
        final Media media = node.getMedia(); // as(Media.class);

        if (media != null)
          {
            downloadable = media.as(Downloadable.class);
            final Status status = downloadable.getStatus();
            final StringBuilder buffer = new StringBuilder(status.getDisplayName());

            if (status == Status.DOWNLOADING)
              {
                buffer.append(String.format(" (%d%%)", Math.round(100 * downloadable.getDownloadProgress())));
              }

            final TextView tvStatus = (TextView)view.findViewById(R.id.tvStatus);
            tvStatus.setText(buffer.toString());
            tvStatus.setTextColor(context.getResources().getColor(COLOR_MAP_BY_STATUS.get(status)));
            final Integer resource = ICON_MAP_BY_ACTION.get(node.getDefaultAction());

            final ImageView ivIcon = (ImageView)view.findViewById(R.id.ivIcon);
            
            if (resource != null)
              {
                ivIcon.setImageResource(resource);
                ivIcon.setVisibility(AndroidUtilities.visibility(true));
              }
            else
              {
                ivIcon.setVisibility(AndroidUtilities.visibility(false));
              }
          }

        return view;
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected static  T get (final @Nonnull Media media, final @Nonnull Key key, final @Nonnull T defaultValue)
      {
        try
          {
            return media.get(key);
          }
        catch (NotFoundException e)
          {
            return defaultValue;
          }
      }

    /*******************************************************************************************************************
     *
     * FIXME: drop this when Media is made to extend EntityWithProperties
     *
     ******************************************************************************************************************/
    protected void renderLicense (final @Nonnull TextView tv, final @Nonnull Media media)
      {
        try
          {
            tv.setText(NbBundle.getMessage(AndroidMediaNodeRenderer.class, "licenseShort-" + media.get(Media.RIGHTS)));
          }
        catch (MissingResourceException e)
          {
            tv.setText("???");
          }
        catch (Exception e)
          {
            tv.setText(NbBundle.getMessage(AndroidMediaNodeRenderer.class, "unknownLicense"));
          }
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    protected void renderLicense (final @Nonnull TextView tv, final @Nonnull EntityWithProperties entity)
      {
        String licenseName = NbBundle.getMessage(AndroidMediaNodeRenderer.class, "unknownLicense");
        String resourceName = null;

        try
          {
            resourceName = "licenseShort-" + entity.get(FactSheet.DC_RIGHTS);
            licenseName = NbBundle.getMessage(AndroidMediaNodeRenderer.class, resourceName);
          }
        catch (NotFoundException e)
          {
            System.err.println("dc:rights not found");
          }
        catch (Exception e)
          {
            System.err.println("license not found: " + resourceName);
          }

        tv.setText(licenseName);
      }

    /*******************************************************************************************************************
     *
     *
     ******************************************************************************************************************/
    @Nonnull
    protected String formatDuration (final @Nonnegative int duration)
      {
        return String.format("%02d:%02d", duration / 60, duration % 60);
      }
  }




© 2015 - 2024 Weber Informatics LLC | Privacy Policy