Ejemplo n.º 1
0
/** Created by Jesus Christ. Amen. */
public class MediaAdapter extends BindedListAdapter<Message, MediaAdapter.MediaHolder> {
  private static final int tileSize =
      Screen.getWidth()
          / AppContext.getContext().getResources().getInteger(R.integer.gallery_items_count);
  private final Context context;
  private final OnMediaClickListener onItemClickListener;

  public MediaAdapter(
      BindedDisplayList<Message> mediaDisplayList,
      OnMediaClickListener onItemClickedListener,
      Context context) {
    super(mediaDisplayList);
    this.context = context;
    this.onItemClickListener = onItemClickedListener;
  }

  @Override
  public MediaHolder onCreateViewHolder(ViewGroup viewGroup, int viewType) {
    return new MediaHolder(
        LayoutInflater.from(context).inflate(R.layout.adapter_media, viewGroup, false));
  }

  @Override
  public void onBindViewHolder(final MediaHolder mediaHolder, int index, final Message item) {
    mediaHolder.bind(item, index, context);
    mediaHolder.itemView.setOnClickListener(
        new View.OnClickListener() {
          @Override
          public void onClick(View v) {
            onItemClickListener.onClick(mediaHolder, item);
          }
        });
  }

  public static class MediaHolder extends BindedViewHolder {
    private final FastThumbLoader fastThumbLoader;
    private MediaPreview previewView;
    private TextView descView;
    private TextView durationView;
    private View videoIndicator;

    public MediaHolder(View itemView) {
      super(itemView);
      previewView = (MediaPreview) itemView.findViewById(R.id.image);
      durationView = (TextView) itemView.findViewById(R.id.duration);
      videoIndicator = itemView.findViewById(R.id.video);
      ViewGroup.LayoutParams params = itemView.getLayoutParams();
      params.height = tileSize;
      params.width = tileSize;
      itemView.setLayoutParams(params);
      fastThumbLoader = new FastThumbLoader(previewView);
      fastThumbLoader.setBlur(5);
    }

    public void bind(Message message, int position, Context context) {
      Bitmap img = null;
      if (!(message.getContent() instanceof DocumentContent)) {
        previewView.setImageBitmap(null);
        return;
      }
      DocumentContent media = ((DocumentContent) message.getContent());
      if (media.getFastThumb() != null) fastThumbLoader.request(media.getFastThumb().getImage());
      if (media instanceof VideoContent) {
        durationView.setText(
            messenger().getFormatter().formatDuration(((VideoContent) media).getDuration()));
        videoIndicator.setVisibility(View.VISIBLE);
        durationView.setVisibility(View.VISIBLE);
      } else {
        videoIndicator.setVisibility(View.GONE);
        durationView.setVisibility(View.GONE);
      }
      if (media.getSource() instanceof FileLocalSource) {
        // todo дичайший outOfMemory неясно где остаётся битмап
        // previewView.requestPhoto(downloaded.getDownloadedPath(), tileSize);

        /*previewView.setImageURI(Uri.fromFile(
        new File(((FileLocalSource) media.getSource()).getFileDescriptor())));*/
      }
    }
  }
}
 public static StickerRecentController getInstance() {
   return getInstance(AppContext.getContext());
 }