MtpThumbnailTileView.java revision c8a9e86919dca8938948f97efc3dcbe143e806bf
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
17package com.android.gallery3d.ingest.ui;
18
19import android.content.Context;
20import android.graphics.Bitmap;
21import android.graphics.Canvas;
22import android.graphics.Paint;
23import android.mtp.MtpDevice;
24import android.mtp.MtpObjectInfo;
25import android.util.AttributeSet;
26import android.widget.Checkable;
27
28import com.android.gallery3d.R;
29import com.android.gallery3d.ingest.data.MtpBitmapFetch;
30
31
32public class MtpThumbnailTileView extends MtpImageView implements Checkable {
33
34    private Paint mForegroundPaint;
35    private boolean mIsChecked;
36    private Bitmap mBitmap;
37
38    private void init() {
39        mForegroundPaint = new Paint();
40        mForegroundPaint.setColor(getResources().getColor(R.color.ingest_highlight_semitransparent));
41    }
42
43    public MtpThumbnailTileView(Context context) {
44        super(context);
45        init();
46    }
47
48    public MtpThumbnailTileView(Context context, AttributeSet attrs) {
49        super(context, attrs);
50        init();
51    }
52
53    public MtpThumbnailTileView(Context context, AttributeSet attrs, int defStyle) {
54        super(context, attrs, defStyle);
55        init();
56    }
57
58    @Override
59    public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
60        // Force this to be square
61        super.onMeasure(widthMeasureSpec, widthMeasureSpec);
62    }
63
64    @Override
65    protected Object fetchMtpImageDataFromDevice(MtpDevice device, MtpObjectInfo info) {
66        return MtpBitmapFetch.getThumbnail(device, info);
67    }
68
69    @Override
70    protected void onMtpImageDataFetchedFromDevice(Object result) {
71        mBitmap = (Bitmap)result;
72        setImageBitmap(mBitmap);
73    }
74
75    @Override
76    public void draw(Canvas canvas) {
77        super.draw(canvas);
78        if (isChecked()) {
79            canvas.drawRect(canvas.getClipBounds(), mForegroundPaint);
80        }
81    }
82
83    @Override
84    public boolean isChecked() {
85        return mIsChecked;
86    }
87
88    @Override
89    public void setChecked(boolean checked) {
90        mIsChecked = checked;
91    }
92
93    @Override
94    public void toggle() {
95        setChecked(!mIsChecked);
96    }
97
98    @Override
99    protected void cancelLoadingAndClear() {
100        super.cancelLoadingAndClear();
101        if (mBitmap != null) {
102            MtpBitmapFetch.recycleThumbnail(mBitmap);
103            mBitmap = null;
104        }
105    }
106}
107