FileDocCategorySizeDatePackage
GalleryPicker.javaAPI DocAndroid 1.5 API28257Wed May 06 22:42:42 BST 2009com.android.camera

GalleryPicker

public class GalleryPicker extends android.app.Activity

Fields Summary
private static final String
TAG
private android.view.View
mNoImagesView
android.widget.GridView
mGridView
android.graphics.drawable.Drawable
mFrameGalleryMask
android.graphics.drawable.Drawable
mCellOutline
android.graphics.drawable.Drawable
mVideoOverlay
android.content.BroadcastReceiver
mReceiver
GalleryPickerAdapter
mAdapter
android.app.Dialog
mMediaScanningDialog
android.content.SharedPreferences
mPrefs
boolean
mPausing
private static long
LOW_STORAGE_THRESHOLD
android.os.Handler
mHandler
Constructors Summary
public GalleryPicker()


      
    
Methods Summary
private intbucketItemCount(int mediaTypes, java.lang.String bucketId)

        // TODO: Find a more efficient way of calculating this
        ImageManager.IImageList list = createImageList(mediaTypes, bucketId);
        try {
            return list.getCount();
        }
        finally {
            list.deactivate();
        }
    
private ImageManager.IImageListcreateImageList(int mediaTypes, java.lang.String bucketId)

        return ImageManager.instance().allImages(
                this,
                getContentResolver(),
                ImageManager.DataLocation.ALL,
                mediaTypes,
                ImageManager.SORT_DESCENDING,
                bucketId);
    
private booleanisEmptyBucket(int mediaTypes, java.lang.String bucketId)

        // TODO: Find a more efficient way of calculating this
        ImageManager.IImageList list = createImageList(mediaTypes, bucketId);
        try {
            return list.isEmpty();
        }
        finally {
            list.deactivate();
        }
    
private voidlaunchFolderGallery(int position)

        mAdapter.mItems.get(position).launch(this);
    
private android.graphics.BitmapmakeMiniThumbBitmap(int width, int height, ImageManager.IImageList images)

        int count = images.getCount();
        // We draw three different version of the folder image depending on the number of images in the folder.
        //    For a single image, that image draws over the whole folder.
        //    For two or three images, we draw the two most recent photos.
        //    For four or more images, we draw four photos.
        final int padding = 4;
        int imageWidth = width;
        int imageHeight = height;
        int offsetWidth = 0;
        int offsetHeight = 0;

        imageWidth = (imageWidth - padding) / 2;     // 2 here because we show two images
        imageHeight = (imageHeight - padding) / 2;   // per row and column

        final Paint  p = new Paint();
        final Bitmap b = Bitmap.createBitmap(width, height, Bitmap.Config.ARGB_8888);
        final Canvas c = new Canvas(b);

        final Matrix m = new Matrix();

        // draw the whole canvas as transparent
        p.setColor(0x00000000);
        c.drawPaint(p);

        // draw the mask normally
        p.setColor(0xFFFFFFFF);
        mFrameGalleryMask.setBounds(0, 0, width, height);
        mFrameGalleryMask.draw(c);

        Paint pdpaint = new Paint();
        pdpaint.setXfermode(new android.graphics.PorterDuffXfermode(
                                    android.graphics.PorterDuff.Mode.SRC_IN));

        pdpaint.setStyle(Paint.Style.FILL);
        c.drawRect(0, 0, width, height, pdpaint);

        for (int i = 0; i < 4; i++) {
            if (mPausing) {
                return null;
            }

            Bitmap temp = null;
            ImageManager.IImage image = i < count ? images.getImageAt(i) : null;

            if (image != null) {
                temp = image.miniThumbBitmap();
            }

            if (temp != null) {
                if (ImageManager.isVideo(image)) {
                    Bitmap newMap = temp.copy(temp.getConfig(), true);
                    Canvas overlayCanvas = new Canvas(newMap);
                    int overlayWidth = mVideoOverlay.getIntrinsicWidth();
                    int overlayHeight = mVideoOverlay.getIntrinsicHeight();
                    int left = (newMap.getWidth() - overlayWidth) / 2;
                    int top = (newMap.getHeight() - overlayHeight) / 2;
                    Rect newBounds = new Rect(left, top, left + overlayWidth, top + overlayHeight);
                    mVideoOverlay.setBounds(newBounds);
                    mVideoOverlay.draw(overlayCanvas);
                    temp.recycle();
                    temp = newMap;
                }

                Bitmap temp2 = ImageLoader.transform(m, temp, imageWidth, imageHeight, true);
                if (temp2 != temp)
                    temp.recycle();
                temp = temp2;
            }

            Bitmap thumb = Bitmap.createBitmap(imageWidth, imageHeight, Bitmap.Config.ARGB_8888);
            Canvas tempCanvas = new Canvas(thumb);
            if (temp != null)
                tempCanvas.drawBitmap(temp, new Matrix(), new Paint());
            mCellOutline.setBounds(0, 0, imageWidth, imageHeight);
            mCellOutline.draw(tempCanvas);

            placeImage(thumb, c, pdpaint, imageWidth, padding, imageHeight, padding, offsetWidth, offsetHeight, i);

            thumb.recycle();

            if (temp != null)
                temp.recycle();
        }

        return b;
    
public voidonCreate(android.os.Bundle icicle)

        super.onCreate(icicle);

        mPrefs = PreferenceManager.getDefaultSharedPreferences(this);

        setContentView(R.layout.gallerypicker);

        mNoImagesView = findViewById(R.id.no_images);
        mGridView = (GridView) findViewById(R.id.albums);
        mGridView.setSelector(android.R.color.transparent);

        mReceiver = new BroadcastReceiver() {

            @Override
            public void onReceive(Context context, Intent intent) {
                if (Config.LOGV) Log.v(TAG, "onReceiveIntent " + intent.getAction());
                String action = intent.getAction();
                if (action.equals(Intent.ACTION_MEDIA_MOUNTED)) {
                    // SD card available
                    // TODO put up a "please wait" message
                    // TODO also listen for the media scanner finished message
                } else if (action.equals(Intent.ACTION_MEDIA_UNMOUNTED)) {
                    // SD card unavailable
                    if (Config.LOGV) Log.v(TAG, "sd card no longer available");
                    Toast.makeText(GalleryPicker.this, getResources().getString(R.string.wait), 5000);
                    rebake(true, false);
                } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_STARTED)) {
                    Toast.makeText(GalleryPicker.this, getResources().getString(R.string.wait), 5000);
                    rebake(false, true);
                } else if (action.equals(Intent.ACTION_MEDIA_SCANNER_FINISHED)) {
                    if (Config.LOGV)
                        Log.v(TAG, "rebake because of ACTION_MEDIA_SCANNER_FINISHED");
                    rebake(false, false);
                } else if (action.equals(Intent.ACTION_MEDIA_EJECT)) {
                    if (Config.LOGV)
                        Log.v(TAG, "rebake because of ACTION_MEDIA_EJECT");
                    rebake(true, false);
                }
            }
        };

        mGridView.setOnItemClickListener(new AdapterView.OnItemClickListener() {
            public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
                launchFolderGallery(position);
            }
        });
        mGridView.setOnCreateContextMenuListener(new View.OnCreateContextMenuListener() {
            public void onCreateContextMenu(ContextMenu menu, View v, final ContextMenu.ContextMenuInfo menuInfo) {
                int position = ((AdapterContextMenuInfo)menuInfo).position;
                menu.setHeaderTitle(mAdapter.baseTitleForPosition(position));
                if ((mAdapter.getIncludeMediaTypes(position) & ImageManager.INCLUDE_IMAGES) != 0) {
                    menu.add(0, 207, 0, R.string.slide_show)
                    .setOnMenuItemClickListener(new OnMenuItemClickListener() {
                        public boolean onMenuItemClick(MenuItem item) {
                            AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
                            int position = info.position;

                            Uri targetUri;
                            synchronized (mAdapter.mItems) {
                                if (position < 0 || position >= mAdapter.mItems.size()) {
                                    return true;
                                }
                                // the mFirstImageUris list includes the "all" uri
                                targetUri = mAdapter.mItems.get(position).mFirstImageUri;
                            }
                            if (targetUri != null && position > 0) {
                                targetUri = targetUri.buildUpon().appendQueryParameter("bucketId",
                                        mAdapter.mItems.get(info.position).mId).build();
                            }
    //                      Log.v(TAG, "URI to launch slideshow " + targetUri);
                            Intent intent = new Intent(Intent.ACTION_VIEW, targetUri);
                            intent.putExtra("slideshow", true);
                            startActivity(intent);
                            return true;
                        }
                    });
                }
                menu.add(0, 208, 0, R.string.view)
                .setOnMenuItemClickListener(new OnMenuItemClickListener() {
                    public boolean onMenuItemClick(MenuItem item) {
                        AdapterView.AdapterContextMenuInfo info = (AdapterView.AdapterContextMenuInfo)menuInfo;
                        launchFolderGallery(info.position);
                        return true;
                    }
                });
            }
        });
 
        ImageManager.ensureOSXCompatibleFolder();
    
public booleanonCreateOptionsMenu(android.view.Menu menu)

        super.onCreateOptionsMenu(menu);

        MenuHelper.addCaptureMenuItems(menu, this);

        menu.add(0, 0, 5, R.string.camerasettings)
        .setOnMenuItemClickListener(new OnMenuItemClickListener() {
            public boolean onMenuItemClick(MenuItem item) {
                Intent preferences = new Intent();
                preferences.setClass(GalleryPicker.this, GallerySettings.class);
                startActivity(preferences);
                return true;
            }
        })
        .setAlphabeticShortcut('p")
        .setIcon(android.R.drawable.ic_menu_preferences);

        return true;
    
public voidonPause()

        super.onPause();
        mPausing = true;
        unregisterReceiver(mReceiver);

        // free up some ram
        mAdapter = null;
        mGridView.setAdapter(null);
        System.gc();
    
public voidonResume()

        super.onResume();
        mPausing = false;

        mAdapter = new GalleryPickerAdapter();
        mGridView.setAdapter(mAdapter);
        setBackgrounds(getResources());

        boolean scanning = ImageManager.isMediaScannerScanning(this);
        rebake(false, scanning);

        // install an intent filter to receive SD card related events.
        IntentFilter intentFilter = new IntentFilter(Intent.ACTION_MEDIA_MOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_UNMOUNTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_STARTED);
        intentFilter.addAction(Intent.ACTION_MEDIA_SCANNER_FINISHED);
        intentFilter.addAction(Intent.ACTION_MEDIA_EJECT);
        intentFilter.addDataScheme("file");

        registerReceiver(mReceiver, intentFilter);
        MenuHelper.requestOrientation(this, mPrefs);
    
private voidplaceImage(android.graphics.Bitmap image, android.graphics.Canvas c, android.graphics.Paint paint, int imageWidth, int widthPadding, int imageHeight, int heightPadding, int offsetX, int offsetY, int pos)


                          
        int row = pos / 2;
        int col = pos - (row * 2);

        int xPos = (col * (imageWidth + widthPadding)) - offsetX;
        int yPos = (row * (imageHeight + heightPadding)) - offsetY;

        c.drawBitmap(image, xPos, yPos, paint);
    
private voidrebake(boolean unmounted, boolean scanning)

        if (mMediaScanningDialog != null) {
            mMediaScanningDialog.cancel();
            mMediaScanningDialog = null;
        }
        if (scanning) {
            mMediaScanningDialog = ProgressDialog.show(
                    this,
                    null,
                    getResources().getString(R.string.wait),
                    true,
                    true);
        }
        if (mAdapter != null) {
            mAdapter.notifyDataSetChanged();
            mAdapter.init(!unmounted && !scanning);
        }

        if (!unmounted) {
            // Warn the user if space is getting low
            Thread t = new Thread(new Runnable() {
                public void run() {

                    // Check available space only if we are writable
                    if (ImageManager.hasStorage()) {
                        String storageDirectory = Environment.getExternalStorageDirectory().toString();
                        StatFs stat = new StatFs(storageDirectory);
                        long remaining = (long)stat.getAvailableBlocks() * (long)stat.getBlockSize();
                        if (remaining < LOW_STORAGE_THRESHOLD) {

                            mHandler.post(new Runnable() {
                                public void run() {
                                    Toast.makeText(GalleryPicker.this.getApplicationContext(),
                                        R.string.not_enough_space, 5000).show();
                                }
                            });
                        }
                    }
                }
            });
            t.start();
        }

        // If we just have zero or one folder, open it. (We shouldn't have just one folder
        // any more, but we can have zero folders.)
        mNoImagesView.setVisibility(View.GONE);
        if (!scanning) {
            int numItems = mAdapter.mItems.size();
            if (numItems == 0) {
                mNoImagesView.setVisibility(View.VISIBLE);
            } else if (numItems == 1) {
                mAdapter.mItems.get(0).launch(this);
                finish();
                return;
            }
        }
    
private voidsetBackgrounds(android.content.res.Resources r)

        mFrameGalleryMask = r.getDrawable(R.drawable.frame_gallery_preview_album_mask);

        mCellOutline = r.getDrawable(android.R.drawable.gallery_thumb);
        mVideoOverlay = r.getDrawable(R.drawable.ic_gallery_video_overlay);