2015-03-31 15:44:41 -07:00
|
|
|
package org.thoughtcrime.securesms.mms;
|
|
|
|
|
|
|
|
import android.content.Context;
|
|
|
|
import android.net.Uri;
|
2015-11-27 14:45:07 -08:00
|
|
|
import android.support.annotation.NonNull;
|
2017-10-11 17:12:46 -07:00
|
|
|
import android.support.annotation.Nullable;
|
2015-03-31 15:44:41 -07:00
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
import com.bumptech.glide.load.Key;
|
|
|
|
import com.bumptech.glide.load.Options;
|
2015-03-31 15:44:41 -07:00
|
|
|
import com.bumptech.glide.load.model.ModelLoader;
|
|
|
|
import com.bumptech.glide.load.model.ModelLoaderFactory;
|
2017-10-11 17:12:46 -07:00
|
|
|
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
|
2015-03-31 15:44:41 -07:00
|
|
|
|
|
|
|
import org.thoughtcrime.securesms.crypto.MasterSecret;
|
|
|
|
import org.thoughtcrime.securesms.mms.DecryptableStreamUriLoader.DecryptableUri;
|
|
|
|
|
|
|
|
import java.io.InputStream;
|
2017-10-11 17:12:46 -07:00
|
|
|
import java.security.MessageDigest;
|
|
|
|
|
|
|
|
public class DecryptableStreamUriLoader implements ModelLoader<DecryptableUri, InputStream> {
|
2015-03-31 15:44:41 -07:00
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
private DecryptableStreamUriLoader(Context context) {
|
|
|
|
this.context = context;
|
|
|
|
}
|
|
|
|
|
|
|
|
@Nullable
|
|
|
|
@Override
|
|
|
|
public LoadData<InputStream> buildLoadData(DecryptableUri decryptableUri, int width, int height, Options options) {
|
|
|
|
return new LoadData<>(decryptableUri, new DecryptableStreamLocalUriFetcher(context, decryptableUri.masterSecret, decryptableUri.uri));
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public boolean handles(DecryptableUri decryptableUri) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
static class Factory implements ModelLoaderFactory<DecryptableUri, InputStream> {
|
|
|
|
|
|
|
|
private final Context context;
|
|
|
|
|
|
|
|
Factory(Context context) {
|
|
|
|
this.context = context.getApplicationContext();
|
|
|
|
}
|
2015-03-31 15:44:41 -07:00
|
|
|
|
|
|
|
@Override
|
2017-10-11 17:12:46 -07:00
|
|
|
public ModelLoader<DecryptableUri, InputStream> build(MultiModelLoaderFactory multiFactory) {
|
2015-03-31 15:44:41 -07:00
|
|
|
return new DecryptableStreamUriLoader(context);
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public void teardown() {
|
|
|
|
// Do nothing.
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
public static class DecryptableUri implements Key {
|
2015-11-27 14:45:07 -08:00
|
|
|
public @NonNull MasterSecret masterSecret;
|
|
|
|
public @NonNull Uri uri;
|
2015-03-31 15:44:41 -07:00
|
|
|
|
2015-11-27 14:45:07 -08:00
|
|
|
public DecryptableUri(@NonNull MasterSecret masterSecret, @NonNull Uri uri) {
|
2015-03-31 15:44:41 -07:00
|
|
|
this.masterSecret = masterSecret;
|
|
|
|
this.uri = uri;
|
|
|
|
}
|
2015-11-27 14:45:07 -08:00
|
|
|
|
2017-10-11 17:12:46 -07:00
|
|
|
@Override
|
|
|
|
public void updateDiskCacheKey(MessageDigest messageDigest) {
|
|
|
|
messageDigest.update(uri.toString().getBytes());
|
|
|
|
}
|
|
|
|
|
2015-11-27 14:45:07 -08:00
|
|
|
@Override
|
|
|
|
public boolean equals(Object o) {
|
|
|
|
if (this == o) return true;
|
|
|
|
if (o == null || getClass() != o.getClass()) return false;
|
|
|
|
|
|
|
|
DecryptableUri that = (DecryptableUri)o;
|
|
|
|
|
|
|
|
return uri.equals(that.uri);
|
|
|
|
|
|
|
|
}
|
|
|
|
|
|
|
|
@Override
|
|
|
|
public int hashCode() {
|
|
|
|
return uri.hashCode();
|
|
|
|
}
|
2015-03-31 15:44:41 -07:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|