OLD | NEW |
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 package org.chromium.chrome.browser.photo_picker; | 5 package org.chromium.chrome.browser.photo_picker; |
6 | 6 |
| 7 import android.support.annotation.IntDef; |
7 import android.support.v7.widget.RecyclerView.Adapter; | 8 import android.support.v7.widget.RecyclerView.Adapter; |
8 import android.support.v7.widget.RecyclerView.ViewHolder; | 9 import android.support.v7.widget.RecyclerView.ViewHolder; |
9 import android.view.LayoutInflater; | 10 import android.view.LayoutInflater; |
10 import android.view.View; | 11 import android.view.View; |
11 import android.view.ViewGroup; | 12 import android.view.ViewGroup; |
12 | 13 |
13 import org.chromium.chrome.R; | 14 import org.chromium.chrome.R; |
14 | 15 |
| 16 import java.lang.annotation.Retention; |
| 17 import java.lang.annotation.RetentionPolicy; |
| 18 |
15 /** | 19 /** |
16 * A data adapter for the Photo Picker. | 20 * A data adapter for the Photo Picker. |
17 */ | 21 */ |
18 public class PickerAdapter extends Adapter<ViewHolder> { | 22 public class PickerAdapter extends Adapter<ViewHolder> { |
| 23 // The possible types of actions required during decoding. |
| 24 @IntDef({NO_ACTION, FROM_CACHE, DECODE}) |
| 25 @Retention(RetentionPolicy.SOURCE) |
| 26 public @interface DecodeActions {} |
| 27 public static final int NO_ACTION = 0; // Gallery/Camera tile: No action. |
| 28 public static final int FROM_CACHE = 1; // Image already decoded. |
| 29 public static final int DECODE = 2; // Image needed to be decoded. |
| 30 |
19 // The category view to use to show the images. | 31 // The category view to use to show the images. |
20 private PickerCategoryView mCategoryView; | 32 private PickerCategoryView mCategoryView; |
21 | 33 |
| 34 // How many times the (high-res) cache was useful. |
| 35 @DecodeActions |
| 36 private int mCacheHits; |
| 37 |
| 38 // How many times a decoding was requested. |
| 39 @DecodeActions |
| 40 private int mDecodeRequests; |
| 41 |
22 /** | 42 /** |
23 * The PickerAdapter constructor. | 43 * The PickerAdapter constructor. |
24 * @param categoryView The category view to use to show the images. | 44 * @param categoryView The category view to use to show the images. |
25 */ | 45 */ |
26 public PickerAdapter(PickerCategoryView categoryView) { | 46 public PickerAdapter(PickerCategoryView categoryView) { |
27 mCategoryView = categoryView; | 47 mCategoryView = categoryView; |
28 } | 48 } |
29 | 49 |
30 // RecyclerView.Adapter: | 50 // RecyclerView.Adapter: |
31 | 51 |
32 @Override | 52 @Override |
33 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { | 53 public ViewHolder onCreateViewHolder(ViewGroup parent, int viewType) { |
34 View itemView = LayoutInflater.from(parent.getContext()) | 54 View itemView = LayoutInflater.from(parent.getContext()) |
35 .inflate(R.layout.photo_picker_bitmap_view, pare
nt, false); | 55 .inflate(R.layout.photo_picker_bitmap_view, pare
nt, false); |
36 PickerBitmapView bitmapView = (PickerBitmapView) itemView; | 56 PickerBitmapView bitmapView = (PickerBitmapView) itemView; |
37 bitmapView.setCategoryView(mCategoryView); | 57 bitmapView.setCategoryView(mCategoryView); |
38 return new PickerBitmapViewHolder(bitmapView); | 58 return new PickerBitmapViewHolder(bitmapView); |
39 } | 59 } |
40 | 60 |
41 @Override | 61 @Override |
42 public void onBindViewHolder(ViewHolder holder, int position) { | 62 public void onBindViewHolder(ViewHolder holder, int position) { |
43 if (holder instanceof PickerBitmapViewHolder) { | 63 if (holder instanceof PickerBitmapViewHolder) { |
44 PickerBitmapViewHolder myHolder = (PickerBitmapViewHolder) holder; | 64 PickerBitmapViewHolder myHolder = (PickerBitmapViewHolder) holder; |
45 myHolder.displayItem(mCategoryView, position); | 65 int result = myHolder.displayItem(mCategoryView, position); |
| 66 if (result == FROM_CACHE) { |
| 67 mCacheHits++; |
| 68 } else if (result == DECODE) { |
| 69 mDecodeRequests++; |
| 70 } |
46 } | 71 } |
47 } | 72 } |
48 | 73 |
49 @Override | 74 @Override |
50 public int getItemCount() { | 75 public int getItemCount() { |
51 return mCategoryView.getPickerBitmaps().size(); | 76 return mCategoryView.getPickerBitmaps().size(); |
52 } | 77 } |
| 78 |
| 79 /** Returns the number of times the cache supplied a bitmap. */ |
| 80 public int getCacheHitCount() { |
| 81 return mCacheHits; |
| 82 } |
| 83 |
| 84 /** Returns the number of decode requests (cache-misses). */ |
| 85 public int getDecodeRequestCount() { |
| 86 return mDecodeRequests; |
| 87 } |
53 } | 88 } |
OLD | NEW |