ImageRecord.java revision 760fe581f6750edd81df4d86cead28a5b9070620
1/*
2 * Copyright (C) 2010 The Android Open Source Project
3 *
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 *      http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17package com.android.apps.tag.record;
18
19import com.android.apps.tag.R;
20import com.google.common.base.Preconditions;
21
22import android.app.Activity;
23import android.content.Context;
24import android.content.Intent;
25import android.content.pm.PackageManager;
26import android.content.pm.ResolveInfo;
27import android.database.Cursor;
28import android.graphics.Bitmap;
29import android.graphics.BitmapFactory;
30import android.nfc.NdefRecord;
31import android.provider.MediaStore;
32import android.view.LayoutInflater;
33import android.view.View;
34import android.view.ViewGroup;
35import android.widget.ImageView;
36import android.widget.TextView;
37
38import java.util.List;
39
40/**
41 * A NdefRecord corresponding to an image type.
42 */
43public class ImageRecord implements ParsedNdefRecord {
44
45    public static final String RECORD_TYPE = "ImageRecord";
46
47    private final Bitmap mBitmap;
48
49    private ImageRecord(Bitmap bitmap) {
50        mBitmap = Preconditions.checkNotNull(bitmap);
51    }
52
53    @Override
54    public View getView(Activity activity, LayoutInflater inflater, ViewGroup parent) {
55        ImageView image = (ImageView) inflater.inflate(R.layout.tag_image, parent, false);
56        image.setImageBitmap(mBitmap);
57        return image;
58    }
59
60    /**
61     * Returns a view in a list of record types for adding new records to a message.
62     */
63    public static View getAddView(Context context, LayoutInflater inflater, ViewGroup parent) {
64        ViewGroup root = (ViewGroup) inflater.inflate(R.layout.tag_image_picker, parent, false);
65
66        // Determine which Activity can retrieve images.
67        Intent intent = new Intent(Intent.ACTION_GET_CONTENT);
68        intent.addCategory(Intent.CATEGORY_OPENABLE);
69        intent.setType("image/*");
70
71        PackageManager pm = context.getPackageManager();
72        List<ResolveInfo> activities = pm.queryIntentActivities(intent, 0);
73        if (activities.isEmpty()) {
74            return null;
75        }
76
77        ResolveInfo info = activities.get(0);
78        ((ImageView) root.findViewById(R.id.image)).setImageDrawable(info.loadIcon(pm));
79        ((TextView) root.findViewById(R.id.text)).setText(context.getString(R.string.photo));
80
81        root.setTag(new ImageRecordEditInfo(context, intent));
82        return root;
83    }
84
85    public static ImageRecord parse(NdefRecord record) {
86        MimeRecord underlyingRecord = MimeRecord.parse(record);
87        Preconditions.checkArgument(underlyingRecord.getMimeType().startsWith("image/"));
88
89        // Try to ensure it's a legal, valid image
90        byte[] content = underlyingRecord.getContent();
91        Bitmap bitmap = BitmapFactory.decodeByteArray(content, 0, content.length);
92        if (bitmap == null) {
93            throw new IllegalArgumentException("not a valid image file");
94        }
95        return new ImageRecord(bitmap);
96    }
97
98    public static boolean isImage(NdefRecord record) {
99        try {
100            parse(record);
101            return true;
102        } catch (IllegalArgumentException e) {
103            return false;
104        }
105    }
106
107    private static class ImageRecordEditInfo extends RecordEditInfo {
108        private final Context mContext;
109        private final Intent mIntent;
110
111        public ImageRecordEditInfo(Context context, Intent intent) {
112            super(RECORD_TYPE);
113            mContext = context;
114            mIntent = intent;
115        }
116
117        @Override
118        public Intent getPickIntent() {
119            return mIntent;
120        }
121
122        @Override
123        public ParsedNdefRecord handlePickResult(Intent data) {
124            Cursor cursor = null;
125            try {
126                String[] projection = {MediaStore.Images.Media.DATA};
127                cursor = mContext.getContentResolver().query(
128                        data.getData(), projection, null, null, null);
129                int index = cursor.getColumnIndexOrThrow(MediaStore.Images.Media.DATA);
130                cursor.moveToFirst();
131                String path = cursor.getString(index);
132
133                // TODO: verify size limits.
134                return new ImageRecord(BitmapFactory.decodeFile(path));
135
136            } catch (IllegalArgumentException ex) {
137                return null;
138
139            } finally {
140                if (cursor != null) {
141                    cursor.close();
142                }
143            }
144        }
145    }
146}
147