1package com.android.wallpaperpicker.tileinfo;
2
3import android.Manifest;
4import android.content.Context;
5import android.content.Intent;
6import android.content.pm.PackageManager;
7import android.database.Cursor;
8import android.graphics.Bitmap;
9import android.graphics.PorterDuff;
10import android.os.Process;
11import android.provider.MediaStore;
12import android.view.LayoutInflater;
13import android.view.View;
14import android.view.ViewGroup;
15import android.widget.ImageView;
16
17import com.android.wallpaperpicker.R;
18import com.android.wallpaperpicker.WallpaperPickerActivity;
19
20public class PickImageInfo extends WallpaperTileInfo {
21
22    @Override
23    public void onClick(WallpaperPickerActivity a) {
24        Intent intent = new Intent(Intent.ACTION_GET_CONTENT).setType("image/*");
25        a.startActivityForResultSafely(intent, WallpaperPickerActivity.IMAGE_PICK);
26    }
27
28    @Override
29    public View createView(Context context, LayoutInflater inflator, ViewGroup parent) {
30        mView = inflator.inflate(R.layout.wallpaper_picker_image_picker_item, parent, false);
31
32        // Make its background the last photo taken on external storage
33        Bitmap lastPhoto = getThumbnailOfLastPhoto(context);
34        if (lastPhoto != null) {
35            ImageView galleryThumbnailBg =
36                    (ImageView) mView.findViewById(R.id.wallpaper_image);
37            galleryThumbnailBg.setImageBitmap(lastPhoto);
38            int colorOverlay = context.getResources().getColor(R.color.wallpaper_picker_translucent_gray);
39            galleryThumbnailBg.setColorFilter(colorOverlay, PorterDuff.Mode.SRC_ATOP);
40        }
41
42        mView.setTag(this);
43        return mView;
44    }
45
46    private Bitmap getThumbnailOfLastPhoto(Context context) {
47        boolean canReadExternalStorage = context.checkPermission(
48                Manifest.permission.READ_EXTERNAL_STORAGE, Process.myPid(), Process.myUid()) ==
49                PackageManager.PERMISSION_GRANTED;
50
51        if (!canReadExternalStorage) {
52            // MediaStore.Images.Media.EXTERNAL_CONTENT_URI requires
53            // the READ_EXTERNAL_STORAGE permission
54            return null;
55        }
56
57        Cursor cursor = MediaStore.Images.Media.query(context.getContentResolver(),
58                MediaStore.Images.Media.EXTERNAL_CONTENT_URI,
59                new String[] { MediaStore.Images.ImageColumns._ID,
60                    MediaStore.Images.ImageColumns.DATE_TAKEN},
61                null, null, MediaStore.Images.ImageColumns.DATE_TAKEN + " DESC LIMIT 1");
62
63        Bitmap thumb = null;
64        if (cursor != null) {
65            if (cursor.moveToNext()) {
66                int id = cursor.getInt(0);
67                thumb = MediaStore.Images.Thumbnails.getThumbnail(context.getContentResolver(),
68                        id, MediaStore.Images.Thumbnails.MINI_KIND, null);
69            }
70            cursor.close();
71        }
72        return thumb;
73    }
74}