Skip to content

Commit 5fadc88

Browse files
committed
Update giphy connectivity strategy for glide
// FREEBIE
1 parent e7937fd commit 5fadc88

File tree

6 files changed

+413
-6
lines changed

6 files changed

+413
-6
lines changed

src/org/thoughtcrime/securesms/giph/model/GiphyImage.java

+12
Original file line numberDiff line numberDiff line change
@@ -12,10 +12,18 @@ public String getGifUrl() {
1212
return images.downsized.url;
1313
}
1414

15+
public long getGifSize() {
16+
return images.downsized.size;
17+
}
18+
1519
public String getGifMmsUrl() {
1620
return images.fixed_height_downsampled.url;
1721
}
1822

23+
public long getMmsGifSize() {
24+
return images.fixed_height_downsampled.size;
25+
}
26+
1927
public float getGifAspectRatio() {
2028
return (float)images.downsized.width / (float)images.downsized.height;
2129
}
@@ -24,6 +32,10 @@ public String getStillUrl() {
2432
return images.downsized_still.url;
2533
}
2634

35+
public long getStillSize() {
36+
return images.downsized_still.size;
37+
}
38+
2739
public static class ImageTypes {
2840
@JsonProperty
2941
private ImageData fixed_height;
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,50 @@
1+
package org.thoughtcrime.securesms.giph.model;
2+
3+
4+
import android.support.annotation.NonNull;
5+
6+
import com.bumptech.glide.load.Key;
7+
8+
import org.thoughtcrime.securesms.util.Conversions;
9+
10+
import java.security.MessageDigest;
11+
12+
public class GiphyPaddedUrl implements Key {
13+
14+
private final String target;
15+
private final long size;
16+
17+
public GiphyPaddedUrl(@NonNull String target, long size) {
18+
this.target = target;
19+
this.size = size;
20+
}
21+
22+
public String getTarget() {
23+
return target;
24+
}
25+
26+
public long getSize() {
27+
return size;
28+
}
29+
30+
@Override
31+
public void updateDiskCacheKey(MessageDigest messageDigest) {
32+
messageDigest.update(target.getBytes());
33+
messageDigest.update(Conversions.longToByteArray(size));
34+
}
35+
36+
@Override
37+
public boolean equals(Object other) {
38+
if (other == null || !(other instanceof GiphyPaddedUrl)) return false;
39+
40+
GiphyPaddedUrl that = (GiphyPaddedUrl)other;
41+
42+
return this.target.equals(that.target) && this.size == that.size;
43+
}
44+
45+
@Override
46+
public int hashCode() {
47+
return target.hashCode() ^ (int)size;
48+
}
49+
50+
}

src/org/thoughtcrime/securesms/giph/ui/GiphyAdapter.java

+11-6
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
import org.thoughtcrime.securesms.R;
2525
import org.thoughtcrime.securesms.color.MaterialColor;
2626
import org.thoughtcrime.securesms.giph.model.GiphyImage;
27+
import org.thoughtcrime.securesms.giph.model.GiphyPaddedUrl;
2728
import org.thoughtcrime.securesms.mms.GlideApp;
2829
import org.thoughtcrime.securesms.mms.GlideRequests;
2930
import org.thoughtcrime.securesms.util.Util;
3031
import org.thoughtcrime.securesms.util.ViewUtil;
3132

33+
import java.io.ByteArrayOutputStream;
3234
import java.io.File;
35+
import java.io.PrintWriter;
3336
import java.util.List;
3437
import java.util.concurrent.ExecutionException;
3538

@@ -69,7 +72,7 @@ public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Dra
6972
Log.w(TAG, e);
7073

7174
synchronized (this) {
72-
if (image.getGifUrl().equals(model)) {
75+
if (new GiphyPaddedUrl(image.getGifUrl(), image.getGifSize()).equals(model)) {
7376
this.modelReady = true;
7477
notifyAll();
7578
}
@@ -81,7 +84,7 @@ public boolean onLoadFailed(@Nullable GlideException e, Object model, Target<Dra
8184
@Override
8285
public boolean onResourceReady(Drawable resource, Object model, Target<Drawable> target, DataSource dataSource, boolean isFirstResource) {
8386
synchronized (this) {
84-
if (image.getGifUrl().equals(model)) {
87+
if (new GiphyPaddedUrl(image.getGifUrl(), image.getGifSize()).equals(model)) {
8588
this.modelReady = true;
8689
notifyAll();
8790
}
@@ -99,7 +102,8 @@ public File getFile(boolean forMms) throws ExecutionException, InterruptedExcept
99102
}
100103

101104
return Glide.with(context)
102-
.load(forMms ? image.getGifMmsUrl() : image.getGifUrl())
105+
.load(forMms ? new GiphyPaddedUrl(image.getGifMmsUrl(), image.getMmsGifSize()) :
106+
new GiphyPaddedUrl(image.getGifUrl(), image.getGifSize()))
103107
.downloadOnly(Target.SIZE_ORIGINAL, Target.SIZE_ORIGINAL)
104108
.get();
105109
}
@@ -144,18 +148,19 @@ public void onBindViewHolder(GiphyViewHolder holder, int position) {
144148
holder.gifProgress.setVisibility(View.GONE);
145149

146150
RequestBuilder<Drawable> thumbnailRequest = GlideApp.with(context)
147-
.load(image.getStillUrl())
151+
.load(new GiphyPaddedUrl(image.getStillUrl(), image.getStillSize()))
148152
.diskCacheStrategy(DiskCacheStrategy.ALL);
149153

150154
if (Util.isLowMemory(context)) {
151-
glideRequests.load(image.getStillUrl())
155+
glideRequests.load(new GiphyPaddedUrl(image.getStillUrl(), image.getStillSize()))
152156
.placeholder(new ColorDrawable(Util.getRandomElement(MaterialColor.values()).toConversationColor(context)))
153157
.diskCacheStrategy(DiskCacheStrategy.ALL)
158+
.listener(holder)
154159
.into(holder.thumbnail);
155160

156161
holder.setModelReady();
157162
} else {
158-
glideRequests.load(image.getGifUrl())
163+
glideRequests.load(new GiphyPaddedUrl(image.getGifUrl(), image.getGifSize()))
159164
.thumbnail(thumbnailRequest)
160165
.placeholder(new ColorDrawable(Util.getRandomElement(MaterialColor.values()).toConversationColor(context)))
161166
.diskCacheStrategy(DiskCacheStrategy.ALL)

0 commit comments

Comments
 (0)