MtpThumbnailTileView.java revision f640d379259bb114a50e3200f49961b89d60f2c2
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 com.android.gallery3d.R;
20import com.android.gallery3d.ingest.data.IngestObjectInfo;
21import com.android.gallery3d.ingest.data.MtpBitmapFetch;
22
23import android.content.Context;
24import android.graphics.Bitmap;
25import android.graphics.Canvas;
26import android.graphics.Paint;
27import android.mtp.MtpDevice;
28import android.util.AttributeSet;
29import android.widget.Checkable;
30
31
32/**
33 * View for thumbnail images from an MTP device
34 */
35public class MtpThumbnailTileView extends MtpImageView implements Checkable {
36
37  private Paint mForegroundPaint;
38  private boolean mIsChecked;
39  private Bitmap mBitmap;
40
41  private void init() {
42    mForegroundPaint = new Paint();
43    mForegroundPaint.setColor(
44        getResources().getColor(R.color.ingest_highlight_semitransparent));
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  protected Object fetchMtpImageDataFromDevice(MtpDevice device, IngestObjectInfo info) {
70    return MtpBitmapFetch.getThumbnail(device, info);
71  }
72
73  @Override
74  protected void onMtpImageDataFetchedFromDevice(Object result) {
75    mBitmap = (Bitmap) result;
76    setImageBitmap(mBitmap);
77  }
78
79  @Override
80  public void draw(Canvas canvas) {
81    super.draw(canvas);
82    if (isChecked()) {
83      canvas.drawRect(canvas.getClipBounds(), mForegroundPaint);
84    }
85  }
86
87  @Override
88  public boolean isChecked() {
89    return mIsChecked;
90  }
91
92  @Override
93  public void setChecked(boolean checked) {
94    if (mIsChecked != checked) {
95      mIsChecked = checked;
96      invalidate();
97    }
98  }
99
100  @Override
101  public void toggle() {
102    setChecked(!mIsChecked);
103  }
104
105  @Override
106  protected void cancelLoadingAndClear() {
107    super.cancelLoadingAndClear();
108    if (mBitmap != null) {
109      MtpBitmapFetch.recycleThumbnail(mBitmap);
110      mBitmap = null;
111    }
112  }
113}
114