1 package com.android.wallpaperpicker.tileinfo;
2 
3 import android.Manifest;
4 import android.content.Context;
5 import android.content.Intent;
6 import android.content.pm.PackageManager;
7 import android.database.Cursor;
8 import android.graphics.Bitmap;
9 import android.graphics.PorterDuff;
10 import android.os.Process;
11 import android.provider.MediaStore;
12 import android.view.LayoutInflater;
13 import android.view.View;
14 import android.view.ViewGroup;
15 import android.widget.ImageView;
16 
17 import com.android.wallpaperpicker.R;
18 import com.android.wallpaperpicker.WallpaperPickerActivity;
19 
20 public class PickImageInfo extends WallpaperTileInfo {
21 
22     @Override
onClick(WallpaperPickerActivity a)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
createView(Context context, LayoutInflater inflator, ViewGroup parent)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 
getThumbnailOfLastPhoto(Context context)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 }