71 lines
2.0 KiB
Java
71 lines
2.0 KiB
Java
package org.thoughtcrime.securesms.giph.net;
|
|
|
|
|
|
import android.content.Context;
|
|
import android.net.Uri;
|
|
import android.support.annotation.NonNull;
|
|
import android.support.annotation.Nullable;
|
|
import android.text.TextUtils;
|
|
import android.util.Log;
|
|
|
|
import com.squareup.okhttp.OkHttpClient;
|
|
import com.squareup.okhttp.Request;
|
|
import com.squareup.okhttp.Response;
|
|
|
|
import org.thoughtcrime.securesms.giph.model.GiphyImage;
|
|
import org.thoughtcrime.securesms.giph.model.GiphyResponse;
|
|
import org.thoughtcrime.securesms.util.AsyncLoader;
|
|
import org.thoughtcrime.securesms.util.JsonUtils;
|
|
|
|
import java.io.IOException;
|
|
import java.util.LinkedList;
|
|
import java.util.List;
|
|
|
|
public abstract class GiphyLoader extends AsyncLoader<List<GiphyImage>> {
|
|
|
|
private static final String TAG = GiphyLoader.class.getName();
|
|
|
|
public static int PAGE_SIZE = 100;
|
|
|
|
@Nullable private String searchString;
|
|
|
|
private final OkHttpClient client = new OkHttpClient();
|
|
|
|
protected GiphyLoader(@NonNull Context context, @Nullable String searchString) {
|
|
super(context);
|
|
this.searchString = searchString;
|
|
this.client.setProxySelector(new GiphyProxySelector());
|
|
}
|
|
|
|
@Override
|
|
public List<GiphyImage> loadInBackground() {
|
|
return loadPage(0);
|
|
}
|
|
|
|
public List<GiphyImage> loadPage(int offset) {
|
|
try {
|
|
String url;
|
|
|
|
if (TextUtils.isEmpty(searchString)) url = String.format(getTrendingUrl(), offset);
|
|
else url = String.format(getSearchUrl(), offset, Uri.encode(searchString));
|
|
|
|
Request request = new Request.Builder().url(url).build();
|
|
Response response = client.newCall(request).execute();
|
|
|
|
if (!response.isSuccessful()) {
|
|
throw new IOException("Unexpected code " + response);
|
|
}
|
|
|
|
GiphyResponse giphyResponse = JsonUtils.fromJson(response.body().byteStream(), GiphyResponse.class);
|
|
|
|
return giphyResponse.getData();
|
|
} catch (IOException e) {
|
|
Log.w(TAG, e);
|
|
return new LinkedList<>();
|
|
}
|
|
}
|
|
|
|
protected abstract String getTrendingUrl();
|
|
protected abstract String getSearchUrl();
|
|
}
|