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