1d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu/*
2d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * Copyright (C) 2013 The Android Open Source Project
3d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *
4d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * Licensed under the Apache License, Version 2.0 (the "License");
5d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * you may not use this file except in compliance with the License.
6d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * You may obtain a copy of the License at
7d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *
8d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *      http://www.apache.org/licenses/LICENSE-2.0
9d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu *
10d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * Unless required by applicable law or agreed to in writing, software
11d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * distributed under the License is distributed on an "AS IS" BASIS,
12d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * See the License for the specific language governing permissions and
14d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu * limitations under the License.
15d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu */
16d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
17d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescupackage com.android.gallery3d.ingest.ui;
18d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
19f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport com.android.gallery3d.R;
20f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport com.android.gallery3d.ingest.data.IngestObjectInfo;
21f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescuimport com.android.gallery3d.ingest.data.MtpBitmapFetch;
22f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
23d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.content.Context;
24d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.graphics.Bitmap;
25d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.graphics.Canvas;
26d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.graphics.Paint;
27d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.mtp.MtpDevice;
28d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.util.AttributeSet;
29d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescuimport android.widget.Checkable;
30d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
31c8a9e86919dca8938948f97efc3dcbe143e806bfBobby Georgescu
32f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu/**
33f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu * View for thumbnail images from an MTP device
34f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu */
35c8a9e86919dca8938948f97efc3dcbe143e806bfBobby Georgescupublic class MtpThumbnailTileView extends MtpImageView implements Checkable {
36d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu
37f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private Paint mForegroundPaint;
38f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private boolean mIsChecked;
39f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private Bitmap mBitmap;
40f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
41f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  private void init() {
42f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    mForegroundPaint = new Paint();
43f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    mForegroundPaint.setColor(
44f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu        getResources().getColor(R.color.ingest_highlight_semitransparent));
45f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
46f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
47f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public MtpThumbnailTileView(Context context) {
48f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super(context);
49f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    init();
50f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
51f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
52f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public MtpThumbnailTileView(Context context, AttributeSet attrs) {
53f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super(context, attrs);
54f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    init();
55f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
56f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
57f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public MtpThumbnailTileView(Context context, AttributeSet attrs, int defStyle) {
58f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super(context, attrs, defStyle);
59f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    init();
60f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
61f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
62f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
63f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void onMeasure(int widthMeasureSpec, int heightMeasureSpec) {
64f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    // Force this to be square
65f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super.onMeasure(widthMeasureSpec, widthMeasureSpec);
66f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
67f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
68f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
69f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  protected Object fetchMtpImageDataFromDevice(MtpDevice device, IngestObjectInfo info) {
70f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return MtpBitmapFetch.getThumbnail(device, info);
71f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
72f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
73f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
74f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  protected void onMtpImageDataFetchedFromDevice(Object result) {
75f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    mBitmap = (Bitmap) result;
76f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    setImageBitmap(mBitmap);
77f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
78f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
79f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
80f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void draw(Canvas canvas) {
81f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super.draw(canvas);
82f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (isChecked()) {
83f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      canvas.drawRect(canvas.getClipBounds(), mForegroundPaint);
84d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu    }
85f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
86f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
87f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
88f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public boolean isChecked() {
89f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    return mIsChecked;
90f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
91f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
92f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
93f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void setChecked(boolean checked) {
94f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (mIsChecked != checked) {
95f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mIsChecked = checked;
96f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      invalidate();
97d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu    }
98f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
99f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
100f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
101f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  public void toggle() {
102f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    setChecked(!mIsChecked);
103f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
104f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu
105f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  @Override
106f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  protected void cancelLoadingAndClear() {
107f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    super.cancelLoadingAndClear();
108f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu    if (mBitmap != null) {
109f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      MtpBitmapFetch.recycleThumbnail(mBitmap);
110f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu      mBitmap = null;
111d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu    }
112f640d379259bb114a50e3200f49961b89d60f2c2Bobby Georgescu  }
113d3aac52ffb88ced53413d5eef29c641dd6982267Bobby Georgescu}
114