Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Api dealing with outdated RequestQueue #87

Merged
merged 2 commits into from
Aug 14, 2014
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
@@ -1,6 +1,12 @@
package com.bumptech.glide.samples.flickr;

import android.os.Build;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

import android.os.Bundle;
import android.os.StrictMode;
import android.support.v4.app.Fragment;
Expand All @@ -15,31 +21,15 @@
import android.widget.Button;
import android.widget.EditText;
import android.widget.TextView;

import com.actionbarsherlock.app.ActionBar;
import com.actionbarsherlock.app.SherlockFragmentActivity;
import com.android.volley.RequestQueue;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.resize.ImageManager;
import com.bumptech.glide.resize.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.resize.cache.DiskCache;
import com.bumptech.glide.resize.cache.DiskLruCacheWrapper;
import com.bumptech.glide.resize.cache.LruMemoryCache;
import com.bumptech.glide.samples.flickr.api.Api;
import com.bumptech.glide.samples.flickr.api.Photo;
import com.bumptech.glide.volley.VolleyUrlLoader;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Set;

public class FlickrSearchActivity extends SherlockFragmentActivity {
private static final String CACHE_NAME = "flickr_cache";

private static final String TAG = "FlickrSearchActivity";

private int searchCount = 0;
Expand All @@ -49,7 +39,6 @@ public class FlickrSearchActivity extends SherlockFragmentActivity {
private Set<PhotoViewer> photoViewers = new HashSet<PhotoViewer>();
private List<Photo> currentPhotos = new ArrayList<Photo>();
private View searchLoading;
private RequestQueue requestQueue;

private enum Page {
SMALL,
Expand Down Expand Up @@ -88,28 +77,6 @@ public void onCreate(Bundle savedInstanceState) {
.penaltyLog()
.build());

final Glide glide = Glide.get();
if (!glide.isImageManagerSet()) {
File cacheDir = ImageManager.getPhotoCacheDir(this, CACHE_NAME);

DiskCache diskCache = DiskLruCacheWrapper.get(cacheDir, 50 * 1024 * 1024);

// When we can recycle bitmaps, the smaller our cache is, the more quickly our scrolling will become smooth
// so prefer large bitmap pool and a small cache.
final int safeMemCacheSize = ImageManager.getSafeMemoryCacheSize(this);
glide.setImageManager(new ImageManager.Builder(this)
.setBitmapCompressQuality(70)
.setMemoryCache(new LruMemoryCache(
Build.VERSION.SDK_INT >= 11 ? safeMemCacheSize / 2 : safeMemCacheSize))
.setBitmapPool(new LruBitmapPool(
Build.VERSION.SDK_INT >= 11 ? Math.round(safeMemCacheSize * 1.5f) : safeMemCacheSize))
.setDiskCache(diskCache));
}

requestQueue = Volley.newRequestQueue(this);

glide.register(URL.class, new VolleyUrlLoader.Factory(requestQueue));

searching = findViewById(R.id.searching);
searchLoading = findViewById(R.id.search_loading);
searchTerm = (TextView) findViewById(R.id.search_term);
Expand Down Expand Up @@ -185,7 +152,7 @@ private void executeSearch() {
searchLoading.setVisibility(View.VISIBLE);
searchTerm.setText(getString(R.string.searching_for, searchString));

Api.get(requestQueue).search(searchString, new Api.SearchCallback() {
Api.get(getApplicationContext()).search(searchString, new Api.SearchCallback() {
@Override
public void onSearchCompleted(List<Photo> photos) {
if (currentSearch != searchCount) return;
Expand Down
63 changes: 50 additions & 13 deletions samples/flickr/src/com/bumptech/glide/samples/flickr/api/Api.java
Original file line number Diff line number Diff line change
@@ -1,21 +1,35 @@
package com.bumptech.glide.samples.flickr.api;

import java.io.File;
import java.net.URL;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import android.content.Context;
import android.os.Build;
import android.util.Log;

import com.android.volley.DefaultRetryPolicy;
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 org.json.JSONArray;
import org.json.JSONException;
import org.json.JSONObject;

import java.util.ArrayList;
import java.util.Collections;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import com.android.volley.toolbox.Volley;
import com.bumptech.glide.Glide;
import com.bumptech.glide.resize.ImageManager;
import com.bumptech.glide.resize.bitmap_recycle.LruBitmapPool;
import com.bumptech.glide.resize.cache.DiskCache;
import com.bumptech.glide.resize.cache.DiskLruCacheWrapper;
import com.bumptech.glide.resize.cache.LruMemoryCache;
import com.bumptech.glide.volley.VolleyUrlLoader;

public class Api {
private static Api API;
Expand All @@ -42,6 +56,8 @@ public class Api {
}

private final RequestQueue requestQueue;
private final Context context;
private static final String CACHE_NAME = "flickr_cache";

private static String getSizeKey(int width, int height) {
final int largestEdge = Math.max(width, height);
Expand All @@ -61,15 +77,36 @@ public interface SearchCallback {
public void onSearchFailed(Exception e);
}

public static Api get(RequestQueue requestQueue) {
public static Api get(Context context) {
if (API == null) {
API = new Api(requestQueue);
API = new Api(context);
}
return API;
}

protected Api(RequestQueue requestQueue) {
this.requestQueue = requestQueue;
protected Api(Context context) {
this.context = context;
requestQueue = Volley.newRequestQueue(context);

final Glide glide = Glide.get();
if (!glide.isImageManagerSet()) {
File cacheDir = ImageManager.getPhotoCacheDir(context, CACHE_NAME);

DiskCache diskCache = DiskLruCacheWrapper.get(cacheDir, 50 * 1024 * 1024);

// When we can recycle bitmaps, the smaller our cache is, the more quickly our scrolling will become smooth
// so prefer large bitmap pool and a small cache.
final int safeMemCacheSize = ImageManager.getSafeMemoryCacheSize(context);
glide.setImageManager(new ImageManager.Builder(context)
.setBitmapCompressQuality(70)
.setMemoryCache(new LruMemoryCache(
Build.VERSION.SDK_INT >= 11 ? safeMemCacheSize / 2 : safeMemCacheSize))
.setBitmapPool(new LruBitmapPool(
Build.VERSION.SDK_INT >= 11 ? Math.round(safeMemCacheSize * 1.5f) : safeMemCacheSize))
.setDiskCache(diskCache));
}
glide.register(URL.class, new VolleyUrlLoader.Factory(requestQueue));

}

public static String getPhotoURL(Photo photo, int width, int height) {
Expand Down