Browse Source

detect images to embed via content-type / urlmetadata call

Andrew Dolgov 3 months ago
parent
commit
01384932df
1 changed files with 96 additions and 105 deletions
  1. 96 105
      org.fox.ttirc/src/main/java/org/fox/ttirc/ChannelFragment.java

+ 96 - 105
org.fox.ttirc/src/main/java/org/fox/ttirc/ChannelFragment.java

@@ -90,7 +90,7 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 	private MainActivity m_activity;
 	private HashMap<String, Bitmap> m_emoticonBitmaps = new HashMap<>();
 	private HashMap<String, Emoticon> m_emoticons = new HashMap<>();
-	private ConcurrentHashMap<String, JsonElement> m_urlMetadata = new ConcurrentHashMap<>();
+	private ConcurrentHashMap<String, JsonObject> m_urlMetadata = new ConcurrentHashMap<>();
 	private int m_lastPos;
 
 	private static int[] ColorMapLight = { 0xff00aaaa, 0xff000000, 0xff0000CC,
@@ -507,7 +507,6 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 	}
 
     private class MessageListHolder {
-
         public TextView message;
 		ImageView imageThumbnail;
 		View metadataContainer;
@@ -662,97 +661,55 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 
                     holder.message.setText(span);
 
-                    if (holder.imageThumbnail != null && !m_prefs.getBoolean("disable_thumbnails", false)) {
+					if (holder.imageThumbnail != null) {
+
+						holder.imageThumbnail.setVisibility(View.GONE);
 						holder.metadataContainer.setVisibility(View.GONE);
+						Glide.clear(holder.imageThumbnail);
 
-						Pattern pattern = Pattern.compile("https?://.*\\.(jpe?g|png|gifv?|bmp|mp4|webm)");
+						Pattern pattern = Pattern.compile("https?://[^ ]+");
 						Matcher matcher = pattern.matcher(span);
 
-						Glide.clear(holder.imageThumbnail);
-
 						if (matcher.find()) {
-							holder.imageThumbnail.getLayoutParams().height = (int)(m_screenHeight * 0.25f);
-							holder.imageThumbnail.setVisibility(View.VISIBLE);
-
 							final String url = matcher.group();
-							final String thumbnailUrl = proxyUrlIfNeeded(url, 300);
+							final String ttircUrl = m_prefs.getString("ttirc_url", "");
 
-							holder.imageThumbnail.setOnClickListener(new View.OnClickListener() {
-								@Override
-								public void onClick(View view) {
-									m_activity.openUri(Uri.parse(proxyUrlIfNeeded(url, 0)));
-								}
-							});
-
-							Glide.with(m_activity)
-									.load(thumbnailUrl)
-									.diskCacheStrategy(DiskCacheStrategy.ALL)
-									.skipMemoryCache(false)
-									.listener(new RequestListener<String, GlideDrawable>() {
-										@Override
-										public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
-											holder.imageThumbnail.setVisibility(View.GONE);
-											return false;
-										}
+							if (m_urlMetadata.containsKey(url)) {
+								//Log.d(TAG, "using cached metadata:" + url);
 
-										@Override
-										public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
-											holder.imageThumbnail.setVisibility(View.VISIBLE);
-											return false;
-										}
-									})
-									.into(holder.imageThumbnail);
-						} else {
-							holder.imageThumbnail.setVisibility(View.GONE);
-
-							pattern = Pattern.compile("https?://[^ ]+");
-							matcher = pattern.matcher(span);
-
-							if (matcher.find()) {
-								final String url = matcher.group();
-								final String ttircUrl = m_prefs.getString("ttirc_url", "");
-
-								if (m_urlMetadata.containsKey(url)) {
-									//Log.d(TAG, "using cached metadata:" + url);
-
-									applyUrlMetadata(url, holder);
-									//m_messageAdapter.notifyDataSetChanged();
-								} else {
-									Log.d(TAG, "requesting metadata for:" + url);
-
-									@SuppressLint("StaticFieldLeak") HttpRequestTask metadataTask = new HttpRequestTask(
-											m_activity, ttircUrl + "/backend.php", m_api.getSessionId()) {
-										@Override
-										protected void onPostExecute(JsonElement result) {
-											Log.d(TAG, "got metadata for URL: " + url + " " + result);
-
-											if (result != null) {
-												m_urlMetadata.put(url, result);
-											} else {
-												JsonObject placeholder = new JsonObject();
-												placeholder.addProperty("no-metadata", true);
-
-												m_urlMetadata.put(url, placeholder);
-											}
-
-											m_messageAdapter.notifyDataSetChanged();
-										}
-									};
+								applyUrlMetadata(url, holder);
+							} else {
+								//Log.d(TAG, "requesting metadata for:" + url);
+
+								@SuppressLint("StaticFieldLeak") HttpRequestTask metadataTask = new HttpRequestTask(
+										m_activity, ttircUrl + "/backend.php", m_api.getSessionId()) {
+									@Override
+									protected void onPostExecute(JsonElement result) {
+										//Log.d(TAG, "got metadata for URL: " + url + " " + result);
 
-									HashMap<String, String> params = new HashMap<String, String>() {
-										{
-											put("op", "urlmetadata");
-											put("url", url);
+										if (result != null && result.isJsonObject()) {
+											m_urlMetadata.put(url, result.getAsJsonObject());
+										} else {
+											JsonObject placeholder = new JsonObject();
+											placeholder.addProperty("no-metadata", true);
+
+											m_urlMetadata.put(url, placeholder);
 										}
-									};
 
-									metadataTask.execute(params);
-								}
+										m_messageAdapter.notifyDataSetChanged();
+									}
+								};
+
+								HashMap<String, String> params = new HashMap<String, String>() {
+									{
+										put("op", "urlmetadata");
+										put("url", url);
+									}
+								};
+
+								metadataTask.execute(params);
 							}
 						}
-					} else if (holder.imageThumbnail != null) {
-                    	holder.imageThumbnail.setVisibility(View.GONE);
-						holder.metadataContainer.setVisibility(View.GONE);
 					}
 
 					break;
@@ -783,7 +740,7 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 					public void onClick(String url) {
 						if (m_activity != null) {
 							try {
-								url = proxyUrlIfNeeded(url, 0);
+								url = proxyMediaURL(url, 0);
 
 								m_activity.openUri(Uri.parse(url));
 							} catch (Exception e) {
@@ -801,15 +758,15 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 
 		private void applyUrlMetadata(final String url, final MessageListHolder holder) {
 			try {
-				JsonObject metadata = m_urlMetadata.get(url).getAsJsonObject();
+				JsonObject metadata = m_urlMetadata.get(url);
 
-				if (!metadata.has("title"))
-					return;
+				//Log.d(TAG, "applying metadata for url:" + url + " " + metadata);
 
-				String title = metadata.get("title").getAsString();
+				/* extended URL metadata */
+				if (metadata.has("title")) {
 
-				if (title != null) {
-					String imageUrl = metadata.has("image") ? metadata.get("image").getAsString() : "";
+					String title = metadata.get("title").getAsString();
+					String imageUrl = metadata.has("image") ? metadata.get("image").getAsString() : null;
 					String summary = metadata.has("descr") ? metadata.get("descr").getAsString() : "";
 
 					holder.metadataContainer.setVisibility(View.VISIBLE);
@@ -822,11 +779,57 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 						}
 					});
 
-					Glide.with(m_activity)
-							.load(imageUrl)
-							.diskCacheStrategy(DiskCacheStrategy.ALL)
-							.skipMemoryCache(false)
-							.into(holder.metadataThumbnail);
+					if (imageUrl != null) {
+						imageUrl = proxyMediaURL(imageUrl, 300);
+
+						Glide.with(m_activity)
+								.load(imageUrl)
+								.diskCacheStrategy(DiskCacheStrategy.ALL)
+								.skipMemoryCache(false)
+								.into(holder.metadataThumbnail);
+					}
+
+
+				} else if (metadata.has("content-type")) {
+					/* check if its an image instead  */
+
+					String contentType = metadata.get("content-type").getAsString();
+
+					if (contentType.contains("image/") || contentType.contains("video/")) {
+						//Log.d(TAG, "metadata is image: " + url);
+
+						holder.imageThumbnail.getLayoutParams().height = (int)(m_screenHeight * 0.25f);
+						holder.imageThumbnail.setVisibility(View.VISIBLE);
+
+						final String thumbnailUrl = proxyMediaURL(url, 300);
+
+						holder.imageThumbnail.setOnClickListener(new View.OnClickListener() {
+							@Override
+							public void onClick(View view) {
+								m_activity.openUri(Uri.parse(proxyMediaURL(url, 0)));
+							}
+						});
+
+						Glide.with(m_activity)
+								.load(thumbnailUrl)
+								.diskCacheStrategy(DiskCacheStrategy.ALL)
+								.skipMemoryCache(false)
+								.listener(new RequestListener<String, GlideDrawable>() {
+									@Override
+									public boolean onException(Exception e, String model, Target<GlideDrawable> target, boolean isFirstResource) {
+										holder.imageThumbnail.setVisibility(View.GONE);
+										return false;
+									}
+
+									@Override
+									public boolean onResourceReady(GlideDrawable resource, String model, Target<GlideDrawable> target, boolean isFromMemoryCache, boolean isFirstResource) {
+										holder.imageThumbnail.setVisibility(View.VISIBLE);
+										return false;
+									}
+								})
+								.into(holder.imageThumbnail);
+
+					}
 				}
 
 			} catch (Exception e) {
@@ -929,7 +932,7 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 		}
 	}
 
-	private String proxyUrlIfNeeded(String url, int resize) {
+	private String proxyMediaURL(String url, int resize) {
 
 		SimpleDateFormat df = new SimpleDateFormat("yyyyMMdd", Locale.US);
 		String ymd = df.format(Calendar.getInstance().getTime());
@@ -943,18 +946,6 @@ public class ChannelFragment extends StateSavedFragment implements OnEditorActio
 				"&cid=" + cid + "&resize=" + resize;
 	}
 
-	private boolean isImageUrl(String url) {
-		return url != null && url.toLowerCase().matches(".*?\\.(jpe?g|gif|png|bmp).*?");
-	}
-
-	private boolean isVideoUrl(String url) {
-		return url != null && url.toLowerCase().matches(".*?\\.(gifv|mp4|webm).*?");
-	}
-
-	private boolean isMediaUrl(String url) {
-		return isImageUrl(url) || isVideoUrl(url);
-	}
-
 	@Override
 	public boolean onEditorAction(TextView v, int actionId, KeyEvent event) {
 		if (actionId == EditorInfo.IME_ACTION_SEND || actionId == EditorInfo.IME_NULL ||