aboutsummaryrefslogtreecommitdiff
path: root/samples
diff options
context:
space:
mode:
authorSam Judd <sam@bu.mp>2013-07-18 10:00:08 -0700
committerSam Judd <sam@bu.mp>2013-07-18 10:00:08 -0700
commitbd2bd139f4b2246924dc5546140c97fc14b3c8b5 (patch)
tree017ddb3a1e7a59c4d96749c746edfd1a92721692 /samples
parenta8186fb26cb4eec77182935578dd6cdbe7ffdda8 (diff)
downloadglide-bd2bd139f4b2246924dc5546140c97fc14b3c8b5.tar.gz
Convert flickr sample to use Volley
Diffstat (limited to 'samples')
-rw-r--r--samples/flickr/project.properties1
-rw-r--r--samples/flickr/src/com/bumptech/flickr/FlickrStreamLoader.java6
-rw-r--r--samples/flickr/src/com/bumptech/flickr/api/Api.java24
-rw-r--r--samples/flickr/src/com/bumptech/flickr/api/Downloader.java197
m---------samples/flickr/volley0
5 files changed, 72 insertions, 156 deletions
diff --git a/samples/flickr/project.properties b/samples/flickr/project.properties
index bd7231cc..10d09c2f 100644
--- a/samples/flickr/project.properties
+++ b/samples/flickr/project.properties
@@ -14,3 +14,4 @@
target=android-16
android.library.reference.1=../../library
android.library.reference.2=ActionBarSherlock/library
+android.library.reference.3=volley/
diff --git a/samples/flickr/src/com/bumptech/flickr/FlickrStreamLoader.java b/samples/flickr/src/com/bumptech/flickr/FlickrStreamLoader.java
index 4846af96..9785dd5e 100644
--- a/samples/flickr/src/com/bumptech/flickr/FlickrStreamLoader.java
+++ b/samples/flickr/src/com/bumptech/flickr/FlickrStreamLoader.java
@@ -1,12 +1,12 @@
package com.bumptech.flickr;
+import com.android.volley.Request;
import com.bumptech.flickr.api.Api;
import com.bumptech.flickr.api.Photo;
import com.bumptech.glide.loader.model.BaseModelStreamLoader;
import com.bumptech.glide.loader.opener.FileInputStreamOpener;
import java.io.File;
-import java.util.concurrent.Future;
/**
* An implementation of a ModelStreamLoader that uses a separate class to download images to disk and then uses
@@ -17,7 +17,7 @@ import java.util.concurrent.Future;
public class FlickrStreamLoader extends BaseModelStreamLoader<Photo>{
private final Api flickrApi;
private final File cacheDir;
- private Future current = null;
+ private Request current = null;
public FlickrStreamLoader(Api flickrApi, File cacheDir) {
this.flickrApi = flickrApi;
@@ -38,7 +38,7 @@ public class FlickrStreamLoader extends BaseModelStreamLoader<Photo>{
@Override
public void clear() {
if (current != null) {
- current.cancel(false);
+ current.cancel();
current = null;
}
}
diff --git a/samples/flickr/src/com/bumptech/flickr/api/Api.java b/samples/flickr/src/com/bumptech/flickr/api/Api.java
index 17490741..82f64637 100644
--- a/samples/flickr/src/com/bumptech/flickr/api/Api.java
+++ b/samples/flickr/src/com/bumptech/flickr/api/Api.java
@@ -1,6 +1,7 @@
package com.bumptech.flickr.api;
import android.content.Context;
+import com.android.volley.Request;
import com.bumptech.flickr.R;
import com.bumptech.glide.util.Log;
import org.json.JSONArray;
@@ -8,7 +9,6 @@ import org.json.JSONException;
import org.json.JSONObject;
import java.io.File;
-import java.io.UnsupportedEncodingException;
import java.net.MalformedURLException;
import java.net.URL;
import java.util.ArrayList;
@@ -18,7 +18,6 @@ import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;
-import java.util.concurrent.Future;
/**
* Created with IntelliJ IDEA.
@@ -75,15 +74,15 @@ public class Api {
private Set<String> downloadedFilesNames = new HashSet<String>();
private final String sizeKey;
- public static Api get(Context context) {
+ public static Api get(Context applicationContext) {
if (API == null) {
- API = new Api(context.getResources().getDimensionPixelSize(R.dimen.large_photo_side));
+ API = new Api(applicationContext, applicationContext.getResources().getDimensionPixelSize(R.dimen.large_photo_side));
}
return API;
}
- protected Api(int maxPhotoSize) {
- this.downloader = Downloader.get();
+ protected Api(Context applicationContext, int maxPhotoSize) {
+ this.downloader = Downloader.get(applicationContext);
this.sizeKey = getSizeKey(maxPhotoSize, maxPhotoSize);
}
@@ -110,13 +109,12 @@ public class Api {
public void search(String text, final SearchCallback cb) {
Log.d("API: searching");
- downloader.download(getSearchUrl(text), new Downloader.MemoryCallback() {
+ downloader.download(getSearchUrl(text), new Downloader.StringCallback() {
@Override
- public void onDownloadReady(byte[] data) {
+ public void onDownloadReady(String result) {
try {
- String stringResults = new String(data, "UTF-8");
//cut out initial flickJsonApi(
- JSONObject searchResults = new JSONObject(stringResults.substring(14, stringResults.length()-1));
+ JSONObject searchResults = new JSONObject(result.substring(14, result.length()-1));
JSONArray photos = searchResults.getJSONObject("photos").getJSONArray("photo");
List<Photo> results = new ArrayList<Photo>(photos.length());
for (int i = 0; i < photos.length(); i++) {
@@ -125,17 +123,15 @@ public class Api {
cb.onSearchCompleted(results);
} catch (JSONException e) {
e.printStackTrace();
- } catch (UnsupportedEncodingException e) {
- e.printStackTrace();
}
}
});
}
- public Future downloadPhoto(Photo photo, File cacheDir, final PhotoCallback cb) {
+ public Request downloadPhoto(Photo photo, File cacheDir, final PhotoCallback cb) {
File out = new File(cacheDir.getPath() + File.separator + photo.id + photo.secret + sizeKey);
final String path = out.getPath();
- Future result = null;
+ Request result = null;
if (downloadedFilesNames.contains(path)) {
cb.onDownloadComplete(path);
} else {
diff --git a/samples/flickr/src/com/bumptech/flickr/api/Downloader.java b/samples/flickr/src/com/bumptech/flickr/api/Downloader.java
index 33b0a88a..95c6f271 100644
--- a/samples/flickr/src/com/bumptech/flickr/api/Downloader.java
+++ b/samples/flickr/src/com/bumptech/flickr/api/Downloader.java
@@ -1,21 +1,20 @@
package com.bumptech.flickr.api;
-import android.os.Handler;
-import android.os.HandlerThread;
-
-import java.io.BufferedInputStream;
+import android.content.Context;
+import com.android.volley.NetworkResponse;
+import com.android.volley.Request;
+import com.android.volley.RequestQueue;
+import com.android.volley.Response;
+import com.android.volley.VolleyError;
+import com.android.volley.toolbox.StringRequest;
+import com.android.volley.toolbox.Volley;
+
+import java.io.BufferedOutputStream;
import java.io.File;
+import java.io.FileNotFoundException;
import java.io.FileOutputStream;
import java.io.IOException;
-import java.io.InputStream;
import java.io.OutputStream;
-import java.net.HttpURLConnection;
-import java.net.URL;
-import java.util.ArrayList;
-import java.util.List;
-import java.util.concurrent.ExecutorService;
-import java.util.concurrent.Executors;
-import java.util.concurrent.Future;
/**
* Created with IntelliJ IDEA.
@@ -26,153 +25,73 @@ import java.util.concurrent.Future;
*/
public class Downloader {
private static Downloader DOWNLOADER;
- private final Handler mainHandler;
- private final ExecutorService executor;
+ private final RequestQueue queue;
- static Downloader get() {
+ static Downloader get(Context context) {
if (DOWNLOADER == null) {
- DOWNLOADER = new Downloader();
+ DOWNLOADER = new Downloader(context);
}
return DOWNLOADER;
}
- public interface MemoryCallback {
- public void onDownloadReady(byte[] data);
- }
-
- public interface DiskCallback {
- public void onDownloadReady(String path);
- }
-
- protected Downloader() {
- System.setProperty("http.keepAlive", "false");
- HandlerThread workerThread = new HandlerThread("downloader_thread");
- workerThread.start();
- executor = Executors.newFixedThreadPool(6);
- mainHandler = new Handler();
+ public Downloader(Context context) {
+ queue = Volley.newRequestQueue(context);
}
- private Future post(Runnable runnable) {
- return executor.submit(runnable);
+ public interface StringCallback {
+ public void onDownloadReady(String result);
}
- public void download(String url, MemoryCallback cb) {
- post(new MemoryDownloadWorker(url, cb));
+ public interface DiskCallback {
+ public void onDownloadReady(String path);
}
- public Future download(String url, File out, DiskCallback cb) {
- return post(new DiskDownloadWorker(url, out, cb));
+ public void download(String url, final StringCallback cb) {
+ queue.add(new StringRequest(Request.Method.GET, url, new Response.Listener<String>() {
+ @Override
+ public void onResponse(String response) {
+ cb.onDownloadReady(response);
+ }
+ }, new Response.ErrorListener() {
+ @Override
+ public void onErrorResponse(VolleyError error) {
+ error.printStackTrace();
+ }
+ }));
}
- private class DiskDownloadWorker implements Runnable {
- private final String url;
- private final DiskCallback cb;
- private final File output;
-
- public DiskDownloadWorker(String url, File output, DiskCallback cb) {
- this.url = url;
- this.output = output;
- this.cb = cb;
- }
-
- @Override
- public void run() {
- if (output.exists()) {
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- cb.onDownloadReady(output.getPath());
- }
- });
- return;
+ public Request download(String url, final File out, final DiskCallback cb) {
+ return queue.add(new Request<String>(Request.Method.GET, url, new Response.ErrorListener() {
+ @Override
+ public void onErrorResponse(VolleyError error) {
+ error.printStackTrace();
}
- HttpURLConnection urlConnection = null;
- try {
- final URL targetUrl = new URL(url);
- urlConnection = (HttpURLConnection) targetUrl.openConnection();
- urlConnection.setDoInput(true);
- urlConnection.setDoOutput(false);
- urlConnection.setUseCaches(false);
- urlConnection.setRequestProperty("Connection", "close");
-
- urlConnection.connect();
- InputStream in = new BufferedInputStream(urlConnection.getInputStream());
- OutputStream out = new FileOutputStream(output);
- writeToOutput(in, out);
- out.close();
- in.close();
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- cb.onDownloadReady(output.getPath());
+ }) {
+ @Override
+ protected Response<String> parseNetworkResponse(NetworkResponse response) {
+ OutputStream os = null;
+ try {
+ os = new BufferedOutputStream(new FileOutputStream(out));
+ os.write(response.data);
+ } catch (FileNotFoundException e) {
+ e.printStackTrace();
+ } catch (IOException e) {
+ e.printStackTrace();
+ } finally {
+ if (os != null) {
+ try {
+ os.close();
+ } catch (IOException e) { }
}
- });
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (urlConnection != null) {
- urlConnection.disconnect();
}
+ return Response.success(out.getAbsolutePath(), getCacheEntry());
}
- }
- private void writeToOutput(InputStream in, OutputStream out) throws IOException {
- byte[] buffer = new byte[1024];
- int bytesRead;
- while (((bytesRead = in.read(buffer)) != -1)) {
- out.write(buffer, 0, bytesRead);
+ @Override
+ protected void deliverResponse(String response) {
+ cb.onDownloadReady(response);
}
- }
- }
-
- private class MemoryDownloadWorker implements Runnable {
-
- private final String url;
- private final MemoryCallback cb;
-
- public MemoryDownloadWorker(String url, MemoryCallback cb) {
- this.url = url;
- this.cb = cb;
- }
-
- @Override
- public void run() {
- HttpURLConnection urlConnection = null;
- try {
- final URL targetUrl = new URL(url);
- urlConnection = (HttpURLConnection) targetUrl.openConnection();
- urlConnection.setDoInput(true);
- urlConnection.setDoOutput(false);
- urlConnection.setUseCaches(false);
- urlConnection.setRequestProperty("Connection", "close");
+ });
- urlConnection.connect();
- InputStream in = new BufferedInputStream(urlConnection.getInputStream());
- final List<Byte> data = new ArrayList<Byte>(1024);
- byte[] buffer = new byte[1024];
- int bytesRead;
- while (((bytesRead = in.read(buffer)) != -1)) {
- for (int i = 0; i < bytesRead; i++) {
- data.add(buffer[i]);
- }
- }
- final byte[] result = new byte[data.size()];
- for (int i = 0; i < data.size(); i++) {
- result[i] = data.get(i);
- }
- mainHandler.post(new Runnable() {
- @Override
- public void run() {
- cb.onDownloadReady(result);
- }
- });
- } catch (IOException e) {
- e.printStackTrace();
- } finally {
- if (urlConnection != null) {
- urlConnection.disconnect();
- }
- }
- }
}
}
diff --git a/samples/flickr/volley b/samples/flickr/volley
new file mode 160000
+Subproject ba7d701bc2a0fa657422242537ace9bd63b1479