1.1 --- /dev/null Thu Jan 01 00:00:00 1970 +0000 1.2 +++ b/mobile/android/thirdparty/com/squareup/picasso/Stats.java Wed Dec 31 06:09:35 2014 +0100 1.3 @@ -0,0 +1,143 @@ 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.os.Handler; 1.23 +import android.os.HandlerThread; 1.24 +import android.os.Looper; 1.25 +import android.os.Message; 1.26 + 1.27 +import static android.os.Process.THREAD_PRIORITY_BACKGROUND; 1.28 + 1.29 +class Stats { 1.30 + private static final int CACHE_HIT = 0; 1.31 + private static final int CACHE_MISS = 1; 1.32 + private static final int BITMAP_DECODE_FINISHED = 2; 1.33 + private static final int BITMAP_TRANSFORMED_FINISHED = 3; 1.34 + 1.35 + private static final String STATS_THREAD_NAME = Utils.THREAD_PREFIX + "Stats"; 1.36 + 1.37 + final HandlerThread statsThread; 1.38 + final Cache cache; 1.39 + final Handler handler; 1.40 + 1.41 + long cacheHits; 1.42 + long cacheMisses; 1.43 + long totalOriginalBitmapSize; 1.44 + long totalTransformedBitmapSize; 1.45 + long averageOriginalBitmapSize; 1.46 + long averageTransformedBitmapSize; 1.47 + int originalBitmapCount; 1.48 + int transformedBitmapCount; 1.49 + 1.50 + Stats(Cache cache) { 1.51 + this.cache = cache; 1.52 + this.statsThread = new HandlerThread(STATS_THREAD_NAME, THREAD_PRIORITY_BACKGROUND); 1.53 + this.statsThread.start(); 1.54 + this.handler = new StatsHandler(statsThread.getLooper(), this); 1.55 + } 1.56 + 1.57 + void dispatchBitmapDecoded(Bitmap bitmap) { 1.58 + processBitmap(bitmap, BITMAP_DECODE_FINISHED); 1.59 + } 1.60 + 1.61 + void dispatchBitmapTransformed(Bitmap bitmap) { 1.62 + processBitmap(bitmap, BITMAP_TRANSFORMED_FINISHED); 1.63 + } 1.64 + 1.65 + void dispatchCacheHit() { 1.66 + handler.sendEmptyMessage(CACHE_HIT); 1.67 + } 1.68 + 1.69 + void dispatchCacheMiss() { 1.70 + handler.sendEmptyMessage(CACHE_MISS); 1.71 + } 1.72 + 1.73 + void shutdown() { 1.74 + statsThread.quit(); 1.75 + } 1.76 + 1.77 + void performCacheHit() { 1.78 + cacheHits++; 1.79 + } 1.80 + 1.81 + void performCacheMiss() { 1.82 + cacheMisses++; 1.83 + } 1.84 + 1.85 + void performBitmapDecoded(long size) { 1.86 + originalBitmapCount++; 1.87 + totalOriginalBitmapSize += size; 1.88 + averageOriginalBitmapSize = getAverage(originalBitmapCount, totalOriginalBitmapSize); 1.89 + } 1.90 + 1.91 + void performBitmapTransformed(long size) { 1.92 + transformedBitmapCount++; 1.93 + totalTransformedBitmapSize += size; 1.94 + averageTransformedBitmapSize = getAverage(originalBitmapCount, totalTransformedBitmapSize); 1.95 + } 1.96 + 1.97 + synchronized StatsSnapshot createSnapshot() { 1.98 + return new StatsSnapshot(cache.maxSize(), cache.size(), cacheHits, cacheMisses, 1.99 + totalOriginalBitmapSize, totalTransformedBitmapSize, averageOriginalBitmapSize, 1.100 + averageTransformedBitmapSize, originalBitmapCount, transformedBitmapCount, 1.101 + System.currentTimeMillis()); 1.102 + } 1.103 + 1.104 + private void processBitmap(Bitmap bitmap, int what) { 1.105 + // Never send bitmaps to the handler as they could be recycled before we process them. 1.106 + int bitmapSize = Utils.getBitmapBytes(bitmap); 1.107 + handler.sendMessage(handler.obtainMessage(what, bitmapSize, 0)); 1.108 + } 1.109 + 1.110 + private static long getAverage(int count, long totalSize) { 1.111 + return totalSize / count; 1.112 + } 1.113 + 1.114 + private static class StatsHandler extends Handler { 1.115 + 1.116 + private final Stats stats; 1.117 + 1.118 + public StatsHandler(Looper looper, Stats stats) { 1.119 + super(looper); 1.120 + this.stats = stats; 1.121 + } 1.122 + 1.123 + @Override public void handleMessage(final Message msg) { 1.124 + switch (msg.what) { 1.125 + case CACHE_HIT: 1.126 + stats.performCacheHit(); 1.127 + break; 1.128 + case CACHE_MISS: 1.129 + stats.performCacheMiss(); 1.130 + break; 1.131 + case BITMAP_DECODE_FINISHED: 1.132 + stats.performBitmapDecoded(msg.arg1); 1.133 + break; 1.134 + case BITMAP_TRANSFORMED_FINISHED: 1.135 + stats.performBitmapTransformed(msg.arg1); 1.136 + break; 1.137 + default: 1.138 + Picasso.HANDLER.post(new Runnable() { 1.139 + @Override public void run() { 1.140 + throw new AssertionError("Unhandled stats message." + msg.what); 1.141 + } 1.142 + }); 1.143 + } 1.144 + } 1.145 + } 1.146 +} 1.147 \ No newline at end of file