mobile/android/thirdparty/com/squareup/picasso/Stats.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.

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.os.Handler;
michael@0 20 import android.os.HandlerThread;
michael@0 21 import android.os.Looper;
michael@0 22 import android.os.Message;
michael@0 23
michael@0 24 import static android.os.Process.THREAD_PRIORITY_BACKGROUND;
michael@0 25
michael@0 26 class Stats {
michael@0 27 private static final int CACHE_HIT = 0;
michael@0 28 private static final int CACHE_MISS = 1;
michael@0 29 private static final int BITMAP_DECODE_FINISHED = 2;
michael@0 30 private static final int BITMAP_TRANSFORMED_FINISHED = 3;
michael@0 31
michael@0 32 private static final String STATS_THREAD_NAME = Utils.THREAD_PREFIX + "Stats";
michael@0 33
michael@0 34 final HandlerThread statsThread;
michael@0 35 final Cache cache;
michael@0 36 final Handler handler;
michael@0 37
michael@0 38 long cacheHits;
michael@0 39 long cacheMisses;
michael@0 40 long totalOriginalBitmapSize;
michael@0 41 long totalTransformedBitmapSize;
michael@0 42 long averageOriginalBitmapSize;
michael@0 43 long averageTransformedBitmapSize;
michael@0 44 int originalBitmapCount;
michael@0 45 int transformedBitmapCount;
michael@0 46
michael@0 47 Stats(Cache cache) {
michael@0 48 this.cache = cache;
michael@0 49 this.statsThread = new HandlerThread(STATS_THREAD_NAME, THREAD_PRIORITY_BACKGROUND);
michael@0 50 this.statsThread.start();
michael@0 51 this.handler = new StatsHandler(statsThread.getLooper(), this);
michael@0 52 }
michael@0 53
michael@0 54 void dispatchBitmapDecoded(Bitmap bitmap) {
michael@0 55 processBitmap(bitmap, BITMAP_DECODE_FINISHED);
michael@0 56 }
michael@0 57
michael@0 58 void dispatchBitmapTransformed(Bitmap bitmap) {
michael@0 59 processBitmap(bitmap, BITMAP_TRANSFORMED_FINISHED);
michael@0 60 }
michael@0 61
michael@0 62 void dispatchCacheHit() {
michael@0 63 handler.sendEmptyMessage(CACHE_HIT);
michael@0 64 }
michael@0 65
michael@0 66 void dispatchCacheMiss() {
michael@0 67 handler.sendEmptyMessage(CACHE_MISS);
michael@0 68 }
michael@0 69
michael@0 70 void shutdown() {
michael@0 71 statsThread.quit();
michael@0 72 }
michael@0 73
michael@0 74 void performCacheHit() {
michael@0 75 cacheHits++;
michael@0 76 }
michael@0 77
michael@0 78 void performCacheMiss() {
michael@0 79 cacheMisses++;
michael@0 80 }
michael@0 81
michael@0 82 void performBitmapDecoded(long size) {
michael@0 83 originalBitmapCount++;
michael@0 84 totalOriginalBitmapSize += size;
michael@0 85 averageOriginalBitmapSize = getAverage(originalBitmapCount, totalOriginalBitmapSize);
michael@0 86 }
michael@0 87
michael@0 88 void performBitmapTransformed(long size) {
michael@0 89 transformedBitmapCount++;
michael@0 90 totalTransformedBitmapSize += size;
michael@0 91 averageTransformedBitmapSize = getAverage(originalBitmapCount, totalTransformedBitmapSize);
michael@0 92 }
michael@0 93
michael@0 94 synchronized StatsSnapshot createSnapshot() {
michael@0 95 return new StatsSnapshot(cache.maxSize(), cache.size(), cacheHits, cacheMisses,
michael@0 96 totalOriginalBitmapSize, totalTransformedBitmapSize, averageOriginalBitmapSize,
michael@0 97 averageTransformedBitmapSize, originalBitmapCount, transformedBitmapCount,
michael@0 98 System.currentTimeMillis());
michael@0 99 }
michael@0 100
michael@0 101 private void processBitmap(Bitmap bitmap, int what) {
michael@0 102 // Never send bitmaps to the handler as they could be recycled before we process them.
michael@0 103 int bitmapSize = Utils.getBitmapBytes(bitmap);
michael@0 104 handler.sendMessage(handler.obtainMessage(what, bitmapSize, 0));
michael@0 105 }
michael@0 106
michael@0 107 private static long getAverage(int count, long totalSize) {
michael@0 108 return totalSize / count;
michael@0 109 }
michael@0 110
michael@0 111 private static class StatsHandler extends Handler {
michael@0 112
michael@0 113 private final Stats stats;
michael@0 114
michael@0 115 public StatsHandler(Looper looper, Stats stats) {
michael@0 116 super(looper);
michael@0 117 this.stats = stats;
michael@0 118 }
michael@0 119
michael@0 120 @Override public void handleMessage(final Message msg) {
michael@0 121 switch (msg.what) {
michael@0 122 case CACHE_HIT:
michael@0 123 stats.performCacheHit();
michael@0 124 break;
michael@0 125 case CACHE_MISS:
michael@0 126 stats.performCacheMiss();
michael@0 127 break;
michael@0 128 case BITMAP_DECODE_FINISHED:
michael@0 129 stats.performBitmapDecoded(msg.arg1);
michael@0 130 break;
michael@0 131 case BITMAP_TRANSFORMED_FINISHED:
michael@0 132 stats.performBitmapTransformed(msg.arg1);
michael@0 133 break;
michael@0 134 default:
michael@0 135 Picasso.HANDLER.post(new Runnable() {
michael@0 136 @Override public void run() {
michael@0 137 throw new AssertionError("Unhandled stats message." + msg.what);
michael@0 138 }
michael@0 139 });
michael@0 140 }
michael@0 141 }
michael@0 142 }
michael@0 143 }

mercurial