2 * Copyright (C) 2010 The Android Open Source Project
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
8 * http://www.apache.org/licenses/LICENSE-2.0
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.
17 package com.android.gallery3d.data;
19 import com.android.gallery3d.app.GalleryApp;
20 import com.android.gallery3d.provider.GalleryProvider;
21 import com.android.gallery3d.util.ThreadPool;
22 import com.android.gallery3d.util.ThreadPool.Job;
23 import com.android.gallery3d.util.ThreadPool.JobContext;
25 import android.content.Context;
26 import android.graphics.Bitmap;
27 import android.graphics.BitmapRegionDecoder;
28 import android.hardware.usb.UsbDevice;
29 import android.mtp.MtpObjectInfo;
30 import android.net.Uri;
31 import android.util.Log;
33 import java.text.DateFormat;
34 import java.util.Date;
36 public class MtpImage extends MediaItem {
37 private static final String TAG = "MtpImage";
39 private final int mDeviceId;
40 private int mObjectId;
41 private int mObjectSize;
42 private long mDateTaken;
43 private String mFileName;
44 private final ThreadPool mThreadPool;
45 private final MtpContext mMtpContext;
46 private final MtpObjectInfo mObjInfo;
47 private final int mImageWidth;
48 private final int mImageHeight;
49 private final Context mContext;
51 MtpImage(Path path, GalleryApp application, int deviceId,
52 MtpObjectInfo objInfo, MtpContext mtpContext) {
53 super(path, nextVersionNumber());
54 mContext = application.getAndroidContext();
57 mObjectId = objInfo.getObjectHandle();
58 mObjectSize = objInfo.getCompressedSize();
59 mDateTaken = objInfo.getDateCreated();
60 mFileName = objInfo.getName();
61 mImageWidth = objInfo.getImagePixWidth();
62 mImageHeight = objInfo.getImagePixHeight();
63 mThreadPool = application.getThreadPool();
64 mMtpContext = mtpContext;
67 MtpImage(Path path, GalleryApp app, int deviceId, int objectId, MtpContext mtpContext) {
68 this(path, app, deviceId, MtpDevice.getObjectInfo(mtpContext, deviceId, objectId),
73 public long getDateInMs() {
78 public Job<Bitmap> requestImage(int type) {
79 return new Job<Bitmap>() {
80 public Bitmap run(JobContext jc) {
81 byte[] thumbnail = mMtpContext.getMtpClient().getThumbnail(
82 UsbDevice.getDeviceName(mDeviceId), mObjectId);
83 if (thumbnail == null) {
84 Log.w(TAG, "decoding thumbnail failed");
87 return DecodeUtils.requestDecode(jc, thumbnail, null);
93 public Job<BitmapRegionDecoder> requestLargeImage() {
94 return new Job<BitmapRegionDecoder>() {
95 public BitmapRegionDecoder run(JobContext jc) {
96 byte[] bytes = mMtpContext.getMtpClient().getObject(
97 UsbDevice.getDeviceName(mDeviceId), mObjectId, mObjectSize);
98 return DecodeUtils.requestCreateBitmapRegionDecoder(
99 jc, bytes, 0, bytes.length, false);
104 public byte[] getImageData() {
105 return mMtpContext.getMtpClient().getObject(
106 UsbDevice.getDeviceName(mDeviceId), mObjectId, mObjectSize);
110 public boolean Import() {
111 return mMtpContext.copyFile(UsbDevice.getDeviceName(mDeviceId), mObjInfo);
115 public int getSupportedOperations() {
116 return SUPPORT_FULL_IMAGE | SUPPORT_IMPORT;
119 public void updateContent(MtpObjectInfo info) {
120 if (mObjectId != info.getObjectHandle() || mDateTaken != info.getDateCreated()) {
121 mObjectId = info.getObjectHandle();
122 mDateTaken = info.getDateCreated();
123 mDataVersion = nextVersionNumber();
128 public String getMimeType() {
129 // Currently only JPEG is supported in MTP.
134 public int getMediaType() {
135 return MEDIA_TYPE_IMAGE;
139 public long getSize() {
144 public Uri getContentUri() {
145 return GalleryProvider.getUriFor(mContext, mPath);
149 public MediaDetails getDetails() {
150 MediaDetails details = super.getDetails();
151 DateFormat formater = DateFormat.getDateTimeInstance();
152 details.addDetail(MediaDetails.INDEX_TITLE, mFileName);
153 details.addDetail(MediaDetails.INDEX_DATETIME, formater.format(new Date(mDateTaken)));
154 details.addDetail(MediaDetails.INDEX_WIDTH, mImageWidth);
155 details.addDetail(MediaDetails.INDEX_HEIGHT, mImageHeight);
156 details.addDetail(MediaDetails.INDEX_SIZE, Long.valueOf(mObjectSize));