2 * Copyright (C) 2013 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.ingest.ui;
19 import android.content.Context;
20 import android.graphics.Bitmap;
21 import android.graphics.Canvas;
22 import android.graphics.Paint;
23 import android.mtp.MtpDevice;
24 import android.mtp.MtpObjectInfo;
25 import android.os.AsyncTask;
26 import android.util.AttributeSet;
27 import android.view.View;
28 import android.widget.Checkable;
29 import android.widget.ImageView;
31 import com.android.gallery3d.R;
33 public class MtpThumbnailTileView extends ImageView implements Checkable {
34 private static final int FADE_IN_TIME_MS = 80;
36 private Paint mForegroundPaint;
37 private boolean mIsChecked;
38 private int mObjectHandle;
39 private int mGeneration;
42 mForegroundPaint = new Paint();
43 mForegroundPaint.setColor(getResources().getColor(R.color.ingest_highlight_semitransparent));
47 public MtpThumbnailTileView(Context context) {
52 public MtpThumbnailTileView(Context context, AttributeSet attrs) {
53 super(context, attrs);
57 public MtpThumbnailTileView(Context context, AttributeSet attrs, int defStyle) {
58 super(context, attrs, defStyle);
63 public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
64 // Force this to be square
65 super.onMeasure(widthMeasureSpec, widthMeasureSpec);
69 public void draw(Canvas canvas) {
72 canvas.drawRect(canvas.getClipBounds(), mForegroundPaint);
77 public boolean isChecked() {
82 public void setChecked(boolean checked) {
87 public void toggle() {
88 setChecked(!mIsChecked);
91 private void showPlaceholder() {
95 private LoadThumbnailTask mTask;
97 public void setMtpDeviceAndObjectInfo(MtpDevice device, MtpObjectInfo object, int gen) {
98 int handle = object.getObjectHandle();
99 if (handle == mObjectHandle && gen == mGeneration) {
107 mObjectHandle = handle;
108 Bitmap thumbnail = MtpBitmapCache.getInstanceForDevice(device)
110 if (thumbnail != null) {
112 setImageBitmap(thumbnail);
115 mTask = new LoadThumbnailTask(device);
116 mTask.execute(object);
120 private class LoadThumbnailTask extends AsyncTask<MtpObjectInfo, Void, Bitmap> {
121 private MtpDevice mDevice;
123 public LoadThumbnailTask(MtpDevice device) {
128 protected Bitmap doInBackground(MtpObjectInfo... args) {
129 Bitmap result = null;
130 if (!isCancelled()) {
131 result = MtpBitmapCache.getInstanceForDevice(mDevice).getOrCreate(
132 args[0].getObjectHandle());
139 protected void onPostExecute(Bitmap result) {
140 if (isCancelled() || result == null) {
144 setImageBitmap(result);
145 animate().alpha(1f).setDuration(FADE_IN_TIME_MS);
149 protected void onCancelled() {