FileDocCategorySizeDatePackage
ThumbnailController.javaAPI DocAndroid 1.5 API7612Wed May 06 22:42:42 BST 2009com.android.camera

ThumbnailController

public class ThumbnailController extends Object

Fields Summary
private static final String
TAG
private android.content.ContentResolver
mContentResolver
private android.net.Uri
mUri
private android.graphics.Bitmap
mThumb
private android.widget.ImageView
mButton
private android.graphics.drawable.Drawable
mFrame
private android.graphics.drawable.Drawable[]
mThumbs
private android.graphics.drawable.TransitionDrawable
mThumbTransition
private boolean
mShouldAnimateThumb
private android.view.animation.Animation
mShowButtonAnimation
private boolean
mShouldAnimateButton
private static final int
BUFSIZE
Constructors Summary
public ThumbnailController(android.widget.ImageView button, android.graphics.drawable.Drawable frame, android.content.ContentResolver contentResolver)


    // The "frame" is a drawable we want to put on top of the thumbnail.
        
                             
        mButton = button;
        mFrame = frame;
        mContentResolver = contentResolver;
        mShowButtonAnimation.setDuration(500);
    
Methods Summary
private voidcloseSilently(java.io.Closeable c)

        if (c != null) {
            try {
                c.close();
            } catch (IOException e) {
                // ignore
            }
        }
    
public android.graphics.BitmapgetThumb()

        return mThumb;
    
public android.net.UrigetUri()

        return mUri;
    
public booleanisUriValid()

        if (mUri == null) return false;
        try {
            mContentResolver.openFileDescriptor(mUri, "r").close();
        } catch (Exception ex) {
            return false;
        }
        return true;
    
public booleanloadData(java.lang.String filePath)

        FileInputStream f = null;
        BufferedInputStream b = null;
        DataInputStream d = null;
        try {
            f = new FileInputStream(filePath);
            b = new BufferedInputStream(f, BUFSIZE);
            d = new DataInputStream(b);
            Uri uri = Uri.parse(d.readUTF());
            Bitmap thumb = BitmapFactory.decodeStream(d);
            setData(uri, thumb);
            d.close();
        } catch (IOException e) {
            return false;
        } finally {
            closeSilently(f);
            closeSilently(b);
            closeSilently(d);
        }
        return true;
    
public voidsetData(android.net.Uri uri, android.graphics.Bitmap original)

        // Make sure uri and original are consistently both null or both non-null.
        if (uri == null || original == null) {
            uri = null;
            original = null;
        }
        mUri = uri;
        updateThumb(original);
    
public booleanstoreData(java.lang.String filePath)


    // Stores the data from the specified file.
    // Returns true for success.
        
        if (mUri == null) {
            return false;
        }

        FileOutputStream f = null;
        BufferedOutputStream b = null;
        DataOutputStream d = null;
        try {
            f = new FileOutputStream(filePath);
            b = new BufferedOutputStream(f, BUFSIZE);
            d = new DataOutputStream(b);
            d.writeUTF(mUri.toString());
            mThumb.compress(Bitmap.CompressFormat.PNG, 100, d);
            d.close();
        } catch (IOException e) {
            return false;
        } finally {
            closeSilently(f);
            closeSilently(b);
            closeSilently(d);
        }
        return true;
    
public voidupdateDisplayIfNeeded()

        if (mUri == null) {
            mButton.setVisibility(View.INVISIBLE);
            return;
        }

        if (mShouldAnimateButton) {
            mButton.setVisibility(View.VISIBLE);
            mButton.startAnimation(mShowButtonAnimation);
            mShouldAnimateButton = false;
        }

        if (mShouldAnimateThumb) {
            mThumbTransition.startTransition(500);
            mShouldAnimateThumb = false;
        }
    
private voidupdateThumb(android.graphics.Bitmap original)

        if (original == null) {
            mThumb = null;
            mThumbs = null;
            return;
        }

        // Make the mini-thumb size smaller than the button size so that the image corners
        // don't peek out from the rounded corners of the frame_thumb graphic:
        final int PADDING_WIDTH = 12;
        final int PADDING_HEIGHT = 12;
        LayoutParams layoutParams = mButton.getLayoutParams();
        final int miniThumbWidth = layoutParams.width - 2 * PADDING_WIDTH;
        final int miniThumbHeight = layoutParams.height - 2 * PADDING_HEIGHT;
        mThumb = ImageManager.extractMiniThumb(
                original, miniThumbWidth, miniThumbHeight, false);

        Drawable[] vignetteLayers = new Drawable[2];
        vignetteLayers[0] = mFrame;
        if (mThumbs == null) {
            mThumbs = new Drawable[2];
            mThumbs[1] = new BitmapDrawable(mThumb);
            vignetteLayers[1] = mThumbs[1];
            mShouldAnimateThumb = false;
        } else {
            mThumbs[0] = mThumbs[1];
            mThumbs[1] = new BitmapDrawable(mThumb);
            mThumbTransition = new TransitionDrawable(mThumbs);
            vignetteLayers[1] = mThumbTransition;
            mShouldAnimateThumb = true;
        }

        LayerDrawable mVignette = new LayerDrawable(vignetteLayers);
        mVignette.setLayerInset(1, PADDING_WIDTH, PADDING_HEIGHT,
                PADDING_WIDTH, PADDING_HEIGHT);
        mButton.setImageDrawable(mVignette);

        if (mButton.getVisibility() != View.VISIBLE) {
            mShouldAnimateButton = true;
        }