Begins to patch images
This commit is contained in:
parent
2e6ca33f96
commit
d905d268f4
|
@ -1,5 +1,6 @@
|
||||||
package fr.gouv.etalab.mastodon.activities;
|
package fr.gouv.etalab.mastodon.activities;
|
||||||
|
|
||||||
|
import android.os.Bundle;
|
||||||
import android.support.v7.app.AppCompatActivity;
|
import android.support.v7.app.AppCompatActivity;
|
||||||
|
|
||||||
import org.conscrypt.Conscrypt;
|
import org.conscrypt.Conscrypt;
|
||||||
|
@ -16,4 +17,5 @@ public class BaseActivity extends AppCompatActivity {
|
||||||
Security.addProvider(Conscrypt.newProvider());
|
Security.addProvider(Conscrypt.newProvider());
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
||||||
}
|
}
|
||||||
|
|
|
@ -0,0 +1,57 @@
|
||||||
|
package fr.gouv.etalab.mastodon.client;
|
||||||
|
|
||||||
|
import android.support.annotation.NonNull;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.bumptech.glide.Glide;
|
||||||
|
import com.bumptech.glide.Priority;
|
||||||
|
import com.bumptech.glide.load.DataSource;
|
||||||
|
import com.bumptech.glide.load.data.DataFetcher;
|
||||||
|
import com.bumptech.glide.load.model.GlideUrl;
|
||||||
|
|
||||||
|
import java.io.IOException;
|
||||||
|
import java.io.InputStream;
|
||||||
|
import java.security.KeyManagementException;
|
||||||
|
import java.security.NoSuchAlgorithmException;
|
||||||
|
|
||||||
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Thomas on 13/12/2017.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class CustomStreamFetcher implements DataFetcher<InputStream> {
|
||||||
|
|
||||||
|
private GlideUrl url;
|
||||||
|
|
||||||
|
public CustomStreamFetcher(GlideUrl url) {
|
||||||
|
this.url = url;
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void loadData(Priority priority, DataCallback<? super InputStream> callback) {
|
||||||
|
new HttpsConnection().getPicture(url.toStringUrl());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cleanup() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void cancel() {
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public Class<InputStream> getDataClass() {
|
||||||
|
return InputStream.class;
|
||||||
|
}
|
||||||
|
|
||||||
|
@NonNull
|
||||||
|
@Override
|
||||||
|
public DataSource getDataSource() {
|
||||||
|
return DataSource.REMOTE;
|
||||||
|
}
|
||||||
|
}
|
|
@ -314,6 +314,30 @@ public class HttpsConnection {
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
public InputStream getPicture(final String downloadUrl) {
|
||||||
|
try {
|
||||||
|
URL url = new URL(downloadUrl);
|
||||||
|
httpsURLConnection = (HttpsURLConnection) url.openConnection();
|
||||||
|
httpsURLConnection.setSSLSocketFactory(new TLSSocketFactory());
|
||||||
|
httpsURLConnection.setRequestProperty("User-Agent", Helper.USER_AGENT);
|
||||||
|
int responseCode = httpsURLConnection.getResponseCode();
|
||||||
|
// always check HTTP response code first
|
||||||
|
if (responseCode == HttpURLConnection.HTTP_OK) {
|
||||||
|
String disposition = httpsURLConnection.getHeaderField("Content-Disposition");
|
||||||
|
// opens input stream from the HTTP connection
|
||||||
|
return httpsURLConnection.getInputStream();
|
||||||
|
}
|
||||||
|
httpsURLConnection.disconnect();
|
||||||
|
} catch (IOException | NoSuchAlgorithmException | KeyManagementException e) {
|
||||||
|
e.printStackTrace();
|
||||||
|
}
|
||||||
|
if(httpsURLConnection != null)
|
||||||
|
httpsURLConnection.disconnect();
|
||||||
|
return null;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
public void upload(final InputStream inputStream, final OnRetrieveAttachmentInterface listener) {
|
public void upload(final InputStream inputStream, final OnRetrieveAttachmentInterface listener) {
|
||||||
|
|
||||||
new Thread(new Runnable() {
|
new Thread(new Runnable() {
|
||||||
|
|
|
@ -0,0 +1,56 @@
|
||||||
|
package fr.gouv.etalab.mastodon.client;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.support.annotation.Nullable;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.bumptech.glide.load.Options;
|
||||||
|
import com.bumptech.glide.load.model.GlideUrl;
|
||||||
|
import com.bumptech.glide.load.model.ModelLoader;
|
||||||
|
import com.bumptech.glide.load.model.ModelLoaderFactory;
|
||||||
|
import com.bumptech.glide.load.model.MultiModelLoaderFactory;
|
||||||
|
import com.bumptech.glide.load.model.stream.UrlLoader;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||||
|
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Thomas on 13/12/2017.
|
||||||
|
*/
|
||||||
|
|
||||||
|
public class HttpsUrlLoader implements ModelLoader<GlideUrl, InputStream> {
|
||||||
|
|
||||||
|
|
||||||
|
|
||||||
|
@SuppressWarnings("WeakerAccess")
|
||||||
|
public HttpsUrlLoader() {
|
||||||
|
}
|
||||||
|
|
||||||
|
@Nullable
|
||||||
|
@Override
|
||||||
|
public LoadData<InputStream> buildLoadData(GlideUrl glideUrl, int width, int height, Options options) {
|
||||||
|
return new LoadData<>(glideUrl, new CustomStreamFetcher(glideUrl));
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public boolean handles(GlideUrl glideUrl) {
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
|
||||||
|
|
||||||
|
public static class Factory implements ModelLoaderFactory<GlideUrl, InputStream> {
|
||||||
|
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public ModelLoader<GlideUrl, InputStream> build(MultiModelLoaderFactory multiFactory) {
|
||||||
|
return new HttpsUrlLoader();
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void teardown() {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
|
@ -0,0 +1,33 @@
|
||||||
|
package fr.gouv.etalab.mastodon.client;
|
||||||
|
|
||||||
|
import android.content.Context;
|
||||||
|
import android.util.Log;
|
||||||
|
|
||||||
|
import com.bumptech.glide.Glide;
|
||||||
|
import com.bumptech.glide.GlideBuilder;
|
||||||
|
import com.bumptech.glide.Registry;
|
||||||
|
import com.bumptech.glide.load.model.GlideUrl;
|
||||||
|
import com.bumptech.glide.module.AppGlideModule;
|
||||||
|
import com.bumptech.glide.annotation.GlideModule;
|
||||||
|
|
||||||
|
import java.io.InputStream;
|
||||||
|
|
||||||
|
import fr.gouv.etalab.mastodon.helper.Helper;
|
||||||
|
|
||||||
|
/**
|
||||||
|
* Created by Thomas on 13/12/2017.
|
||||||
|
*/
|
||||||
|
|
||||||
|
@GlideModule
|
||||||
|
public final class TLSLibraryGlideModule extends AppGlideModule {
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void registerComponents(Context context, Glide glide, Registry registry) {
|
||||||
|
registry.replace(GlideUrl.class, InputStream.class, new HttpsUrlLoader.Factory());
|
||||||
|
}
|
||||||
|
|
||||||
|
@Override
|
||||||
|
public void applyOptions(Context context, GlideBuilder builder) {
|
||||||
|
|
||||||
|
}
|
||||||
|
}
|
Loading…
Reference in New Issue