OSDN Git Service

2aeda73db9d931b4cf26ec152c6d0ad9557e8518
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / ingest / ui / MtpThumbnailTileView.java
1 /*
2  * Copyright (C) 2013 The Android Open Source Project
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
17 package com.android.gallery3d.ingest.ui;
18
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;
30
31 import com.android.gallery3d.R;
32
33 public class MtpThumbnailTileView extends ImageView implements Checkable {
34     private static final int FADE_IN_TIME_MS = 80;
35
36     private Paint mForegroundPaint;
37     private boolean mIsChecked;
38     private int mObjectHandle;
39     private int mGeneration;
40
41     private void init() {
42         mForegroundPaint = new Paint();
43         mForegroundPaint.setColor(getResources().getColor(R.color.ingest_highlight_semitransparent));
44         showPlaceholder();
45     }
46
47     public MtpThumbnailTileView(Context context) {
48         super(context);
49         init();
50     }
51
52     public MtpThumbnailTileView(Context context, AttributeSet attrs) {
53         super(context, attrs);
54         init();
55     }
56
57     public MtpThumbnailTileView(Context context, AttributeSet attrs, int defStyle) {
58         super(context, attrs, defStyle);
59         init();
60     }
61
62     @Override
63     public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
64         // Force this to be square
65         super.onMeasure(widthMeasureSpec, widthMeasureSpec);
66     }
67
68     @Override
69     public void draw(Canvas canvas) {
70         super.draw(canvas);
71         if (mIsChecked) {
72             canvas.drawRect(canvas.getClipBounds(), mForegroundPaint);
73         }
74     }
75
76     @Override
77     public boolean isChecked() {
78         return mIsChecked;
79     }
80
81     @Override
82     public void setChecked(boolean checked) {
83         mIsChecked = checked;
84     }
85
86     @Override
87     public void toggle() {
88         setChecked(!mIsChecked);
89     }
90
91     private void showPlaceholder() {
92         setAlpha(0f);
93     }
94
95     private LoadThumbnailTask mTask;
96
97     public void setMtpDeviceAndObjectInfo(MtpDevice device, MtpObjectInfo object, int gen) {
98         int handle = object.getObjectHandle();
99         if (handle == mObjectHandle && gen == mGeneration) {
100             return;
101         }
102         animate().cancel();
103         if (mTask != null) {
104             mTask.cancel(true);
105         }
106         mGeneration = gen;
107         mObjectHandle = handle;
108         Bitmap thumbnail = MtpBitmapCache.getInstanceForDevice(device)
109                 .get(handle);
110         if (thumbnail != null) {
111             setAlpha(1f);
112             setImageBitmap(thumbnail);
113         } else {
114             showPlaceholder();
115             mTask = new LoadThumbnailTask(device);
116             mTask.execute(object);
117         }
118     }
119
120     private class LoadThumbnailTask extends AsyncTask<MtpObjectInfo, Void, Bitmap> {
121         private MtpDevice mDevice;
122
123         public LoadThumbnailTask(MtpDevice device) {
124             mDevice = device;
125         }
126
127         @Override
128         protected Bitmap doInBackground(MtpObjectInfo... args) {
129             Bitmap result = null;
130             if (!isCancelled()) {
131                 result = MtpBitmapCache.getInstanceForDevice(mDevice).getOrCreate(
132                         args[0].getObjectHandle());
133             }
134             mDevice = null;
135             return result;
136         }
137
138         @Override
139         protected void onPostExecute(Bitmap result) {
140             if (isCancelled() || result == null) {
141                 return;
142             }
143             setAlpha(0f);
144             setImageBitmap(result);
145             animate().alpha(1f).setDuration(FADE_IN_TIME_MS);
146         }
147
148         @Override
149         protected void onCancelled() {
150         }
151     }
152 }