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