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

Wed, 31 Dec 2014 07:22:50 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 07:22:50 +0100
branch
TOR_BUG_3246
changeset 4
fc2d59ddac77
permissions
-rw-r--r--

Correct previous dual key logic pending first delivery installment.

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

mercurial