Skip to content

Commit

Permalink
Introduce Network Policy API
Browse files Browse the repository at this point in the history
  • Loading branch information
dnkoutso committed Dec 31, 2014
1 parent ce1df3d commit 8e90794
Show file tree
Hide file tree
Showing 23 changed files with 230 additions and 102 deletions.
14 changes: 12 additions & 2 deletions picasso/src/main/java/com/squareup/picasso/Action.java
Expand Up @@ -37,6 +37,7 @@ public RequestWeakReference(Action action, M referent, ReferenceQueue<? super M>
final WeakReference<T> target;
final boolean noFade;
final int memoryPolicy;
final int networkPolicy;
final int errorResId;
final Drawable errorDrawable;
final String key;
Expand All @@ -45,13 +46,14 @@ public RequestWeakReference(Action action, M referent, ReferenceQueue<? super M>
boolean willReplay;
boolean cancelled;

Action(Picasso picasso, T target, Request request, int memoryPolicy, boolean noFade,
int errorResId, Drawable errorDrawable, String key, Object tag) {
Action(Picasso picasso, T target, Request request, int memoryPolicy, int networkPolicy,
int errorResId, Drawable errorDrawable, String key, Object tag, boolean noFade) {
this.picasso = picasso;
this.request = request;
this.target =
target == null ? null : new RequestWeakReference<T>(this, target, picasso.referenceQueue);
this.memoryPolicy = memoryPolicy;
this.networkPolicy = networkPolicy;
this.noFade = noFade;
this.errorResId = errorResId;
this.errorDrawable = errorDrawable;
Expand Down Expand Up @@ -87,6 +89,14 @@ boolean willReplay() {
return willReplay;
}

int getMemoryPolicy() {
return memoryPolicy;
}

int getNetworkPolicy() {
return networkPolicy;
}

Picasso getPicasso() {
return picasso;
}
Expand Down
6 changes: 4 additions & 2 deletions picasso/src/main/java/com/squareup/picasso/BitmapHunter.java
Expand Up @@ -73,6 +73,7 @@ class BitmapHunter implements Runnable {
final String key;
final Request data;
final int memoryPolicy;
final int networkPolicy;
final RequestHandler requestHandler;

Action action;
Expand All @@ -96,7 +97,8 @@ class BitmapHunter implements Runnable {
this.key = action.getKey();
this.data = action.getRequest();
this.priority = action.getPriority();
this.memoryPolicy = action.memoryPolicy;
this.memoryPolicy = action.getMemoryPolicy();
this.networkPolicy = action.getNetworkPolicy();
this.requestHandler = requestHandler;
this.retryCount = requestHandler.getRetryCount();
}
Expand Down Expand Up @@ -152,7 +154,7 @@ Bitmap hunt() throws IOException {
}
}

data.loadFromLocalCacheOnly = (retryCount == 0);
data.networkPolicy = retryCount == 0 ? NetworkPolicy.OFFLINE.index : networkPolicy;
RequestHandler.Result result = requestHandler.load(data);
if (result != null) {
bitmap = result.getBitmap();
Expand Down
9 changes: 4 additions & 5 deletions picasso/src/main/java/com/squareup/picasso/Downloader.java
Expand Up @@ -26,14 +26,13 @@ public interface Downloader {
* Download the specified image {@code url} from the internet.
*
* @param uri Remote image URL.
* @param localCacheOnly If {@code true} the URL should only be loaded if available in a local
* disk cache.
* @param networkPolicy The {@link NetworkPolicy} used for this request.
* @return {@link Response} containing either a {@link Bitmap} representation of the request or an
* {@link InputStream} for the image data. {@code null} can be returned to indicate a problem
* loading the bitmap.
* @throws IOException if the requested URL cannot successfully be loaded.
*/
Response load(Uri uri, boolean localCacheOnly) throws IOException;
Response load(Uri uri, int networkPolicy) throws IOException;

/**
* Allows to perform a clean up for this {@link Downloader} including closing the disk cache and
Expand All @@ -46,9 +45,9 @@ class ResponseException extends IOException {
final boolean localCacheOnly;
final int responseCode;

public ResponseException(String message, boolean localCacheOnly, int responseCode) {
public ResponseException(String message, int networkPolicy, int responseCode) {
super(message);
this.localCacheOnly = localCacheOnly;
this.localCacheOnly = NetworkPolicy.isOfflineOnly(networkPolicy);
this.responseCode = responseCode;
}
}
Expand Down
5 changes: 3 additions & 2 deletions picasso/src/main/java/com/squareup/picasso/FetchAction.java
Expand Up @@ -21,8 +21,9 @@ class FetchAction extends Action<Object> {

private final Object target;

FetchAction(Picasso picasso, Request data, int memoryPolicy, String key, Object tag) {
super(picasso, null, data, memoryPolicy, false, 0, null, key, tag);
FetchAction(Picasso picasso, Request data, int memoryPolicy, int networkPolicy, Object tag,
String key) {
super(picasso, null, data, memoryPolicy, networkPolicy, 0, null, key, tag, false);
this.target = new Object();
}

Expand Down
5 changes: 3 additions & 2 deletions picasso/src/main/java/com/squareup/picasso/GetAction.java
Expand Up @@ -18,8 +18,9 @@
import android.graphics.Bitmap;

class GetAction extends Action<Void> {
GetAction(Picasso picasso, Request data, int memoryPolicy, String key, Object tag) {
super(picasso, null, data, memoryPolicy, false, 0, null, key, tag);
GetAction(Picasso picasso, Request data, int memoryPolicy, int networkPolicy, Object tag,
String key) {
super(picasso, null, data, memoryPolicy, networkPolicy, 0, null, key, tag, false);
}

@Override void complete(Bitmap result, Picasso.LoadedFrom from) {
Expand Down
Expand Up @@ -25,9 +25,10 @@ class ImageViewAction extends Action<ImageView> {
Callback callback;

ImageViewAction(Picasso picasso, ImageView imageView, Request data, int memoryPolicy,
boolean noFade, int errorResId, Drawable errorDrawable, String key, Object tag,
Callback callback) {
super(picasso, imageView, data, memoryPolicy, noFade, errorResId, errorDrawable, key, tag);
int networkPolicy, int errorResId, Drawable errorDrawable, String key, Object tag,
Callback callback, boolean noFade) {
super(picasso, imageView, data, memoryPolicy, networkPolicy, errorResId, errorDrawable, key,
tag, noFade);
this.callback = callback;
}

Expand Down
32 changes: 32 additions & 0 deletions picasso/src/main/java/com/squareup/picasso/NetworkPolicy.java
@@ -0,0 +1,32 @@
package com.squareup.picasso;

/** TODO */
@SuppressWarnings("PointlessBitwiseExpression") public enum NetworkPolicy {

/** TODO */
NO_CACHE(1 << 0),

/** TODO */
NO_STORE(1 << 1),

/** TODO */
OFFLINE(1 << 2);

static boolean shouldReadFromDiskCache(int networkPolicy) {
return (networkPolicy & NetworkPolicy.NO_CACHE.index) == 0;
}

static boolean shouldWriteToDiskCache(int networkPolicy) {
return (networkPolicy & NetworkPolicy.NO_STORE.index) == 0;
}

static boolean isOfflineOnly(int networkPolicy) {
return (networkPolicy & NetworkPolicy.OFFLINE.index) == NetworkPolicy.OFFLINE.index;
}

final int index;

private NetworkPolicy(int index) {
this.index = index;
}
}
Expand Up @@ -46,7 +46,7 @@ public NetworkRequestHandler(Downloader downloader, Stats stats) {
}

@Override public Result load(Request data) throws IOException {
Response response = downloader.load(data.uri, data.loadFromLocalCacheOnly);
Response response = downloader.load(data.uri, data.networkPolicy);
if (response == null) {
return null;
}
Expand Down
25 changes: 18 additions & 7 deletions picasso/src/main/java/com/squareup/picasso/OkHttpDownloader.java
Expand Up @@ -17,6 +17,7 @@

import android.content.Context;
import android.net.Uri;
import com.squareup.okhttp.CacheControl;
import com.squareup.okhttp.OkHttpClient;
import com.squareup.okhttp.ResponseBody;
import java.io.File;
Expand Down Expand Up @@ -89,19 +90,29 @@ protected final OkHttpClient getClient() {
return client;
}

@Override public Response load(Uri uri, boolean localCacheOnly) throws IOException {
com.squareup.okhttp.Request.Builder requestBuilder =
new com.squareup.okhttp.Request.Builder().url(uri.toString());

if (localCacheOnly) {
requestBuilder.addHeader("Cache-Control", "only-if-cached,max-age=" + Integer.MAX_VALUE);
@Override public Response load(Uri uri, int networkPolicy) throws IOException {
CacheControl cacheControl;
if (NetworkPolicy.isOfflineOnly(networkPolicy)) {
cacheControl = CacheControl.FORCE_CACHE;
} else {
CacheControl.Builder builder = new CacheControl.Builder();
if (!NetworkPolicy.shouldReadFromDiskCache(networkPolicy)) {
builder.noCache();
}
if (!NetworkPolicy.shouldWriteToDiskCache(networkPolicy)) {
builder.noStore();
}
cacheControl = builder.build();
}

com.squareup.okhttp.Request.Builder requestBuilder =
new com.squareup.okhttp.Request.Builder().cacheControl(cacheControl).url(uri.toString());

com.squareup.okhttp.Response response = client.newCall(requestBuilder.build()).execute();
int responseCode = response.code();
if (responseCode >= 300) {
response.body().close();
throw new ResponseException(responseCode + " " + response.message(), localCacheOnly,
throw new ResponseException(responseCode + " " + response.message(), networkPolicy,
responseCode);
}

Expand Down
Expand Up @@ -31,8 +31,8 @@ abstract class RemoteViewsAction extends Action<RemoteViewsAction.RemoteViewsTar
private RemoteViewsTarget target;

RemoteViewsAction(Picasso picasso, Request data, RemoteViews remoteViews, int viewId,
int errorResId, int memoryPolicy, String key, Object tag) {
super(picasso, null, data, memoryPolicy, false, errorResId, null, key, tag);
int errorResId, int memoryPolicy, int networkPolicy, Object tag, String key) {
super(picasso, null, data, memoryPolicy, networkPolicy, errorResId, null, key, tag, false);
this.remoteViews = remoteViews;
this.viewId = viewId;
}
Expand Down Expand Up @@ -88,8 +88,9 @@ static class AppWidgetAction extends RemoteViewsAction {
private final int[] appWidgetIds;

AppWidgetAction(Picasso picasso, Request data, RemoteViews remoteViews, int viewId,
int[] appWidgetIds, int memoryPolicy, int errorResId, String key, Object tag) {
super(picasso, data, remoteViews, viewId, errorResId, memoryPolicy, key, tag);
int[] appWidgetIds, int memoryPolicy, int networkPolicy, String key, Object tag,
int errorResId) {
super(picasso, data, remoteViews, viewId, errorResId, memoryPolicy, networkPolicy, tag, key);
this.appWidgetIds = appWidgetIds;
}

Expand All @@ -104,9 +105,9 @@ static class NotificationAction extends RemoteViewsAction {
private final Notification notification;

NotificationAction(Picasso picasso, Request data, RemoteViews remoteViews, int viewId,
int notificationId, Notification notification, int memoryPolicy, int errorResId,
String key, Object tag) {
super(picasso, data, remoteViews, viewId, errorResId, memoryPolicy, key, tag);
int notificationId, Notification notification, int memoryPolicy, int networkPolicy,
String key, Object tag, int errorResId) {
super(picasso, data, remoteViews, viewId, errorResId, memoryPolicy, networkPolicy, tag, key);
this.notificationId = notificationId;
this.notification = notification;
}
Expand Down
4 changes: 2 additions & 2 deletions picasso/src/main/java/com/squareup/picasso/Request.java
Expand Up @@ -32,8 +32,8 @@ public final class Request {
int id;
/** The time that the request was first submitted (in nanos). */
long started;
/** Whether or not this request should only load from local cache. */
boolean loadFromLocalCacheOnly;
/** TODO */
int networkPolicy;

/**
* The image URI.
Expand Down
37 changes: 29 additions & 8 deletions picasso/src/main/java/com/squareup/picasso/RequestCreator.java
Expand Up @@ -60,6 +60,7 @@ public class RequestCreator {
private int placeholderResId;
private int errorResId;
private int memoryPolicy;
private int networkPolicy;
private Drawable placeholderDrawable;
private Drawable errorDrawable;
private Object tag;
Expand Down Expand Up @@ -322,6 +323,26 @@ public RequestCreator memoryPolicy(MemoryPolicy policy, MemoryPolicy... addition
return this;
}

/** TODO */
public RequestCreator networkPolicy(NetworkPolicy policy, NetworkPolicy... additional) {
if (policy == null) {
throw new IllegalArgumentException("Network policy cannot be null.");
}
this.networkPolicy |= policy.index;
if (additional == null) {
throw new IllegalArgumentException("Network policy cannot be null.");
}
if (additional.length > 0) {
for (NetworkPolicy networkPolicy : additional) {
if (networkPolicy == null) {
throw new IllegalArgumentException("Network policy cannot be null.");
}
this.networkPolicy |= networkPolicy.index;
}
}
return this;
}

/** Disable brief fade in of images loaded from the disk cache or network. */
public RequestCreator noFade() {
noFade = true;
Expand All @@ -348,7 +369,7 @@ public Bitmap get() throws IOException {
Request finalData = createRequest(started);
String key = createKey(finalData, new StringBuilder());

Action action = new GetAction(picasso, finalData, memoryPolicy, key, tag);
Action action = new GetAction(picasso, finalData, memoryPolicy, networkPolicy, tag, key);
return forRequest(picasso, picasso.dispatcher, picasso.cache, picasso.stats, action).hunt();
}

Expand All @@ -373,7 +394,7 @@ public void fetch() {
Request request = createRequest(started);
String key = createKey(request, new StringBuilder());

Action action = new FetchAction(picasso, request, memoryPolicy, key, tag);
Action action = new FetchAction(picasso, request, memoryPolicy, networkPolicy, tag, key);
picasso.submit(action);
}
}
Expand Down Expand Up @@ -455,8 +476,8 @@ public void into(Target target) {
target.onPrepareLoad(setPlaceholder ? getPlaceholderDrawable() : null);

Action action =
new TargetAction(picasso, target, request, memoryPolicy, errorResId, errorDrawable,
requestKey, tag);
new TargetAction(picasso, target, request, memoryPolicy, networkPolicy, errorDrawable,
requestKey, tag, errorResId);
picasso.enqueueAndSubmit(action);
}

Expand Down Expand Up @@ -487,7 +508,7 @@ public void into(RemoteViews remoteViews, int viewId, int notificationId,

RemoteViewsAction action =
new NotificationAction(picasso, request, remoteViews, viewId, notificationId, notification,
memoryPolicy, errorResId, key, tag);
memoryPolicy, networkPolicy, key, tag, errorResId);

performRemoteViewInto(action);
}
Expand Down Expand Up @@ -518,7 +539,7 @@ public void into(RemoteViews remoteViews, int viewId, int[] appWidgetIds) {

RemoteViewsAction action =
new AppWidgetAction(picasso, request, remoteViews, viewId, appWidgetIds, memoryPolicy,
errorResId, key, tag);
networkPolicy, key, tag, errorResId);

performRemoteViewInto(action);
}
Expand Down Expand Up @@ -597,8 +618,8 @@ public void into(ImageView target, Callback callback) {
}

Action action =
new ImageViewAction(picasso, target, request, memoryPolicy, noFade, errorResId,
errorDrawable, requestKey, tag, callback);
new ImageViewAction(picasso, target, request, memoryPolicy, networkPolicy, errorResId,
errorDrawable, requestKey, tag, callback, noFade);

picasso.enqueueAndSubmit(action);
}
Expand Down
7 changes: 4 additions & 3 deletions picasso/src/main/java/com/squareup/picasso/TargetAction.java
Expand Up @@ -20,9 +20,10 @@

final class TargetAction extends Action<Target> {

TargetAction(Picasso picasso, Target target, Request data, int memoryPolicy,
int errorResId, Drawable errorDrawable, String key, Object tag) {
super(picasso, target, data, memoryPolicy, false, errorResId, errorDrawable, key, tag);
TargetAction(Picasso picasso, Target target, Request data, int memoryPolicy, int networkPolicy,
Drawable errorDrawable, String key, Object tag, int errorResId) {
super(picasso, target, data, memoryPolicy, networkPolicy, errorResId, errorDrawable, key, tag,
false);
}

@Override void complete(Bitmap result, Picasso.LoadedFrom from) {
Expand Down

0 comments on commit 8e90794

Please sign in to comment.