mobile/android/thirdparty/com/squareup/picasso/NetworkBitmapHunter.java

changeset 0
6474c204b198
     1.1 --- /dev/null	Thu Jan 01 00:00:00 1970 +0000
     1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/NetworkBitmapHunter.java	Wed Dec 31 06:09:35 2014 +0100
     1.3 @@ -0,0 +1,113 @@
     1.4 +/*
     1.5 + * Copyright (C) 2013 Square, Inc.
     1.6 + *
     1.7 + * Licensed under the Apache License, Version 2.0 (the "License");
     1.8 + * you may not use this file except in compliance with the License.
     1.9 + * You may obtain a copy of the License at
    1.10 + *
    1.11 + *      http://www.apache.org/licenses/LICENSE-2.0
    1.12 + *
    1.13 + * Unless required by applicable law or agreed to in writing, software
    1.14 + * distributed under the License is distributed on an "AS IS" BASIS,
    1.15 + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    1.16 + * See the License for the specific language governing permissions and
    1.17 + * limitations under the License.
    1.18 + */
    1.19 +package com.squareup.picasso;
    1.20 +
    1.21 +import android.graphics.Bitmap;
    1.22 +import android.graphics.BitmapFactory;
    1.23 +import android.net.NetworkInfo;
    1.24 +import java.io.IOException;
    1.25 +import java.io.InputStream;
    1.26 +
    1.27 +import static com.squareup.picasso.Downloader.Response;
    1.28 +import static com.squareup.picasso.Picasso.LoadedFrom.DISK;
    1.29 +import static com.squareup.picasso.Picasso.LoadedFrom.NETWORK;
    1.30 +
    1.31 +class NetworkBitmapHunter extends BitmapHunter {
    1.32 +  static final int DEFAULT_RETRY_COUNT = 2;
    1.33 +  private static final int MARKER = 65536;
    1.34 +
    1.35 +  private final Downloader downloader;
    1.36 +
    1.37 +  int retryCount;
    1.38 +
    1.39 +  public NetworkBitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
    1.40 +      Action action, Downloader downloader) {
    1.41 +    super(picasso, dispatcher, cache, stats, action);
    1.42 +    this.downloader = downloader;
    1.43 +    this.retryCount = DEFAULT_RETRY_COUNT;
    1.44 +  }
    1.45 +
    1.46 +  @Override Bitmap decode(Request data) throws IOException {
    1.47 +    boolean loadFromLocalCacheOnly = retryCount == 0;
    1.48 +
    1.49 +    Response response = downloader.load(data.uri, loadFromLocalCacheOnly);
    1.50 +    if (response == null) {
    1.51 +      return null;
    1.52 +    }
    1.53 +
    1.54 +    loadedFrom = response.cached ? DISK : NETWORK;
    1.55 +
    1.56 +    Bitmap result = response.getBitmap();
    1.57 +    if (result != null) {
    1.58 +      return result;
    1.59 +    }
    1.60 +
    1.61 +    InputStream is = response.getInputStream();
    1.62 +    try {
    1.63 +      return decodeStream(is, data);
    1.64 +    } finally {
    1.65 +      Utils.closeQuietly(is);
    1.66 +    }
    1.67 +  }
    1.68 +
    1.69 +  @Override boolean shouldRetry(boolean airplaneMode, NetworkInfo info) {
    1.70 +    boolean hasRetries = retryCount > 0;
    1.71 +    if (!hasRetries) {
    1.72 +      return false;
    1.73 +    }
    1.74 +    retryCount--;
    1.75 +    return info == null || info.isConnectedOrConnecting();
    1.76 +  }
    1.77 +
    1.78 +  private Bitmap decodeStream(InputStream stream, Request data) throws IOException {
    1.79 +    if (stream == null) {
    1.80 +      return null;
    1.81 +    }
    1.82 +    MarkableInputStream markStream = new MarkableInputStream(stream);
    1.83 +    stream = markStream;
    1.84 +
    1.85 +    long mark = markStream.savePosition(MARKER);
    1.86 +
    1.87 +    boolean isWebPFile = Utils.isWebPFile(stream);
    1.88 +    markStream.reset(mark);
    1.89 +    // When decode WebP network stream, BitmapFactory throw JNI Exception and make app crash.
    1.90 +    // Decode byte array instead
    1.91 +    if (isWebPFile) {
    1.92 +      byte[] bytes = Utils.toByteArray(stream);
    1.93 +      BitmapFactory.Options options = null;
    1.94 +      if (data.hasSize()) {
    1.95 +        options = new BitmapFactory.Options();
    1.96 +        options.inJustDecodeBounds = true;
    1.97 +
    1.98 +        BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    1.99 +        calculateInSampleSize(data.targetWidth, data.targetHeight, options);
   1.100 +      }
   1.101 +      return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
   1.102 +    } else {
   1.103 +      BitmapFactory.Options options = null;
   1.104 +      if (data.hasSize()) {
   1.105 +        options = new BitmapFactory.Options();
   1.106 +        options.inJustDecodeBounds = true;
   1.107 +
   1.108 +        BitmapFactory.decodeStream(stream, null, options);
   1.109 +        calculateInSampleSize(data.targetWidth, data.targetHeight, options);
   1.110 +
   1.111 +        markStream.reset(mark);
   1.112 +      }
   1.113 +      return BitmapFactory.decodeStream(stream, null, options);
   1.114 +    }
   1.115 +  }
   1.116 +}

mercurial