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

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

     1 /*
     2  * Copyright (C) 2013 Square, Inc.
     3  *
     4  * Licensed under the Apache License, Version 2.0 (the "License");
     5  * you may not use this file except in compliance with the License.
     6  * You may obtain a copy of the License at
     7  *
     8  *      http://www.apache.org/licenses/LICENSE-2.0
     9  *
    10  * Unless required by applicable law or agreed to in writing, software
    11  * distributed under the License is distributed on an "AS IS" BASIS,
    12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    13  * See the License for the specific language governing permissions and
    14  * limitations under the License.
    15  */
    16 package com.squareup.picasso;
    18 import android.graphics.Bitmap;
    19 import android.graphics.BitmapFactory;
    20 import android.net.NetworkInfo;
    21 import java.io.IOException;
    22 import java.io.InputStream;
    24 import static com.squareup.picasso.Downloader.Response;
    25 import static com.squareup.picasso.Picasso.LoadedFrom.DISK;
    26 import static com.squareup.picasso.Picasso.LoadedFrom.NETWORK;
    28 class NetworkBitmapHunter extends BitmapHunter {
    29   static final int DEFAULT_RETRY_COUNT = 2;
    30   private static final int MARKER = 65536;
    32   private final Downloader downloader;
    34   int retryCount;
    36   public NetworkBitmapHunter(Picasso picasso, Dispatcher dispatcher, Cache cache, Stats stats,
    37       Action action, Downloader downloader) {
    38     super(picasso, dispatcher, cache, stats, action);
    39     this.downloader = downloader;
    40     this.retryCount = DEFAULT_RETRY_COUNT;
    41   }
    43   @Override Bitmap decode(Request data) throws IOException {
    44     boolean loadFromLocalCacheOnly = retryCount == 0;
    46     Response response = downloader.load(data.uri, loadFromLocalCacheOnly);
    47     if (response == null) {
    48       return null;
    49     }
    51     loadedFrom = response.cached ? DISK : NETWORK;
    53     Bitmap result = response.getBitmap();
    54     if (result != null) {
    55       return result;
    56     }
    58     InputStream is = response.getInputStream();
    59     try {
    60       return decodeStream(is, data);
    61     } finally {
    62       Utils.closeQuietly(is);
    63     }
    64   }
    66   @Override boolean shouldRetry(boolean airplaneMode, NetworkInfo info) {
    67     boolean hasRetries = retryCount > 0;
    68     if (!hasRetries) {
    69       return false;
    70     }
    71     retryCount--;
    72     return info == null || info.isConnectedOrConnecting();
    73   }
    75   private Bitmap decodeStream(InputStream stream, Request data) throws IOException {
    76     if (stream == null) {
    77       return null;
    78     }
    79     MarkableInputStream markStream = new MarkableInputStream(stream);
    80     stream = markStream;
    82     long mark = markStream.savePosition(MARKER);
    84     boolean isWebPFile = Utils.isWebPFile(stream);
    85     markStream.reset(mark);
    86     // When decode WebP network stream, BitmapFactory throw JNI Exception and make app crash.
    87     // Decode byte array instead
    88     if (isWebPFile) {
    89       byte[] bytes = Utils.toByteArray(stream);
    90       BitmapFactory.Options options = null;
    91       if (data.hasSize()) {
    92         options = new BitmapFactory.Options();
    93         options.inJustDecodeBounds = true;
    95         BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    96         calculateInSampleSize(data.targetWidth, data.targetHeight, options);
    97       }
    98       return BitmapFactory.decodeByteArray(bytes, 0, bytes.length, options);
    99     } else {
   100       BitmapFactory.Options options = null;
   101       if (data.hasSize()) {
   102         options = new BitmapFactory.Options();
   103         options.inJustDecodeBounds = true;
   105         BitmapFactory.decodeStream(stream, null, options);
   106         calculateInSampleSize(data.targetWidth, data.targetHeight, options);
   108         markStream.reset(mark);
   109       }
   110       return BitmapFactory.decodeStream(stream, null, options);
   111     }
   112   }
   113 }

mercurial