OSDN Git Service

218f7048372f75aafb165a3e239295c407e40a1d
[android-x86/packages-apps-Gallery2.git] / src / com / android / gallery3d / data / MtpImage.java
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
17 package com.android.gallery3d.data;
18
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;
24
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;
32
33 import java.text.DateFormat;
34 import java.util.Date;
35
36 public class MtpImage extends MediaItem {
37     private static final String TAG = "MtpImage";
38
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;
50
51     MtpImage(Path path, GalleryApp application, int deviceId,
52             MtpObjectInfo objInfo, MtpContext mtpContext) {
53         super(path, nextVersionNumber());
54         mContext = application.getAndroidContext();
55         mDeviceId = deviceId;
56         mObjInfo = objInfo;
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;
65     }
66
67     MtpImage(Path path, GalleryApp app, int deviceId, int objectId, MtpContext mtpContext) {
68         this(path, app, deviceId, MtpDevice.getObjectInfo(mtpContext, deviceId, objectId),
69                 mtpContext);
70     }
71
72     @Override
73     public long getDateInMs() {
74         return mDateTaken;
75     }
76
77     @Override
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");
85                     return null;
86                 }
87                 return DecodeUtils.requestDecode(jc, thumbnail, null);
88             }
89         };
90     }
91
92     @Override
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);
100             }
101         };
102     }
103
104     public byte[] getImageData() {
105         return mMtpContext.getMtpClient().getObject(
106                 UsbDevice.getDeviceName(mDeviceId), mObjectId, mObjectSize);
107     }
108
109     @Override
110     public boolean Import() {
111         return mMtpContext.copyFile(UsbDevice.getDeviceName(mDeviceId), mObjInfo);
112     }
113
114     @Override
115     public int getSupportedOperations() {
116         return SUPPORT_FULL_IMAGE | SUPPORT_IMPORT;
117     }
118
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();
124         }
125     }
126
127     @Override
128     public String getMimeType() {
129         // Currently only JPEG is supported in MTP.
130         return "image/jpeg";
131     }
132
133     @Override
134     public int getMediaType() {
135         return MEDIA_TYPE_IMAGE;
136     }
137
138     @Override
139     public long getSize() {
140         return mObjectSize;
141     }
142
143     @Override
144     public Uri getContentUri() {
145         return GalleryProvider.getUriFor(mContext, mPath);
146     }
147
148     @Override
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));
157         return details;
158     }
159
160 }