RocketChatMessageUrlsLayout.java 5.39 KB
Newer Older
Yusuke Iwaki's avatar
Yusuke Iwaki committed
1 2 3 4 5 6 7 8 9 10 11 12 13
package chat.rocket.android.widget.message;

import android.annotation.TargetApi;
import android.content.Context;
import android.content.Intent;
import android.net.Uri;
import android.os.Build;
import android.text.TextUtils;
import android.util.AttributeSet;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.TextView;
14
import chat.rocket.android.widget.helper.FrescoHelper;
15
import com.facebook.drawee.view.SimpleDraweeView;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
16

Tiago Cunha's avatar
Tiago Cunha committed
17 18
import java.util.List;
import java.util.Map;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
19 20
import chat.rocket.android.widget.R;
import chat.rocket.android.widget.helper.ImageFormat;
Tiago Cunha's avatar
Tiago Cunha committed
21 22 23 24
import chat.rocket.core.models.WebContent;
import chat.rocket.core.models.WebContentHeaders;
import chat.rocket.core.models.WebContentMeta;
import chat.rocket.core.models.WebContentParsedUrl;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
25

Yusuke Iwaki's avatar
Yusuke Iwaki committed
26 27 28 29
/**
 */
public class RocketChatMessageUrlsLayout extends LinearLayout {
  private LayoutInflater inflater;
Tiago Cunha's avatar
Tiago Cunha committed
30
  private List<WebContent> webContents;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48

  public RocketChatMessageUrlsLayout(Context context) {
    super(context);
    initialize(context, null);
  }

  public RocketChatMessageUrlsLayout(Context context, AttributeSet attrs) {
    super(context, attrs);
    initialize(context, attrs);
  }

  public RocketChatMessageUrlsLayout(Context context, AttributeSet attrs, int defStyleAttr) {
    super(context, attrs, defStyleAttr);
    initialize(context, attrs);
  }

  @TargetApi(Build.VERSION_CODES.LOLLIPOP)
  public RocketChatMessageUrlsLayout(Context context, AttributeSet attrs, int defStyleAttr,
Yusuke Iwaki's avatar
Yusuke Iwaki committed
49
                                     int defStyleRes) {
Yusuke Iwaki's avatar
Yusuke Iwaki committed
50 51 52 53 54 55 56 57 58
    super(context, attrs, defStyleAttr, defStyleRes);
    initialize(context, attrs);
  }

  private void initialize(Context context, AttributeSet attrs) {
    inflater = LayoutInflater.from(context);
    setOrientation(VERTICAL);
  }

59
  public void setUrls(List<WebContent> webContents, boolean autoloadImages) {
Tiago Cunha's avatar
Tiago Cunha committed
60
    if (this.webContents != null && this.webContents.equals(webContents)) {
61 62
      return;
    }
Tiago Cunha's avatar
Tiago Cunha committed
63 64

    this.webContents = webContents;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
65 66
    removeAllViews();

Tiago Cunha's avatar
Tiago Cunha committed
67
    for (int i = 0, size = webContents.size(); i < size; i++) {
68
      appendUrlView(webContents.get(i), autoloadImages);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
69 70 71
    }
  }

72
  private void appendUrlView(WebContent webContent, boolean autoloadImages) {
Tiago Cunha's avatar
Tiago Cunha committed
73
    final String url = webContent.getUrl();
74

Tiago Cunha's avatar
Tiago Cunha committed
75
    addMessageInlineImage(webContent, autoloadImages, url);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
76 77

    // see Rocket.Chat:packages/rocketchat-oembed/client/oembedUrlWidget.coffee
Tiago Cunha's avatar
Tiago Cunha committed
78 79 80 81
    final Map<WebContentMeta.Type, WebContentMeta> webContentMetaMap = webContent.getMetaMap();
    if (webContentMetaMap == null || webContentMetaMap.size() == 0) {
      return;
    }
Yusuke Iwaki's avatar
Yusuke Iwaki committed
82

83
    final String imageURL = webContent.getMetaImage();
Yusuke Iwaki's avatar
Yusuke Iwaki committed
84

Tiago Cunha's avatar
Tiago Cunha committed
85 86
    WebContentParsedUrl parsedUrl = webContent.getParsedUrl();
    String host = parsedUrl != null ? parsedUrl.getHost() : null;
Yusuke Iwaki's avatar
Yusuke Iwaki committed
87

Tiago Cunha's avatar
Tiago Cunha committed
88
    View embedUrl = inflater.inflate(R.layout.message_inline_embed_url, this, false);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
89

Tiago Cunha's avatar
Tiago Cunha committed
90
    ((TextView) embedUrl.findViewById(R.id.hostname)).setText(host);
Tiago Cunha's avatar
Tiago Cunha committed
91 92
    ((TextView) embedUrl.findViewById(R.id.title)).setText(webContent.getMetaTitle());
    ((TextView) embedUrl.findViewById(R.id.description)).setText(removeQuote(webContent.getMetaDescription()));
Yusuke Iwaki's avatar
Yusuke Iwaki committed
93

Tiago Cunha's avatar
Tiago Cunha committed
94 95 96 97
    final SimpleDraweeView image = (SimpleDraweeView) embedUrl.findViewById(R.id.image);
    if (TextUtils.isEmpty(imageURL)) {
      image.setVisibility(View.GONE);
    } else {
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
98
      FrescoHelper.INSTANCE.loadImageWithCustomization(image, imageURL);
Tiago Cunha's avatar
Tiago Cunha committed
99
      image.setVisibility(View.VISIBLE);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
100
    }
Tiago Cunha's avatar
Tiago Cunha committed
101 102 103 104 105 106 107 108 109 110 111

    embedUrl.setOnClickListener(new View.OnClickListener() {
      @Override
      public void onClick(View v) {
        Intent intent = new Intent(Intent.ACTION_VIEW, Uri.parse(url));
        intent.setFlags(Intent.FLAG_ACTIVITY_NEW_TASK);
        getContext().startActivity(intent);
      }
    });

    addView(embedUrl);
Yusuke Iwaki's avatar
Yusuke Iwaki committed
112
  }
113

Tiago Cunha's avatar
Tiago Cunha committed
114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
  private String removeQuote(String description) {
    if (description != null) {
      if (description.startsWith("\"")) {
        description = description.substring(1);
      }
      if (description.endsWith("\"")) {
        description = description.substring(0, description.length() - 1);
      }
    }
    return description;
  }

  private void addMessageInlineImage(WebContent webContent, boolean autoloadImages, String url) {
    final WebContentHeaders webContentHeaders = webContent.getHeaders();
    String contentType = webContentHeaders != null ? webContentHeaders.getContentType() : "";

    if (contentType != null && contentType.startsWith("image/")
        && ImageFormat.SUPPORTED_LIST.contains(contentType)) {
      final View inlineImageView = inflater.inflate(R.layout.message_inline_image, this, false);
      final SimpleDraweeView
          inlineImage = (SimpleDraweeView) inlineImageView.findViewById(R.id.message_inline_image);
      final View loadView = inlineImageView.findViewById(R.id.message_inline_image_load);

      loadImage(url, inlineImage, loadView, autoloadImages);
      addView(inlineImageView);
    }
  }

142 143 144 145
  private void loadImage(final String url, final SimpleDraweeView drawee, final View load,
                         boolean autoloadImage) {
    if (autoloadImage) {
      load.setVisibility(GONE);
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
146
      FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
147 148 149 150 151 152 153 154
      return;
    }

    load.setOnClickListener(new OnClickListener() {
      @Override
      public void onClick(View v) {
        load.setVisibility(GONE);
        load.setOnClickListener(null);
Filipe de Lima Brito's avatar
Filipe de Lima Brito committed
155
        FrescoHelper.INSTANCE.loadImageWithCustomization(drawee, url);
156 157
      }
    });
158
  }
Yusuke Iwaki's avatar
Yusuke Iwaki committed
159
}