summaryrefslogtreecommitdiffstats
path: root/src/com/android/gallery3d/data/MtpImage.java
blob: 4766d88f8602184f88dd59dedf1eb3dac56d4b16 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
/*
 * Copyright (C) 2010 The Android Open Source Project
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */

package com.android.gallery3d.data;

import com.android.gallery3d.app.GalleryApp;
import com.android.gallery3d.provider.GalleryProvider;
import com.android.gallery3d.util.ThreadPool;
import com.android.gallery3d.util.ThreadPool.Job;
import com.android.gallery3d.util.ThreadPool.JobContext;

import android.graphics.Bitmap;
import android.graphics.BitmapRegionDecoder;
import android.hardware.usb.UsbDevice;
import android.mtp.MtpObjectInfo;
import android.net.Uri;
import android.util.Log;

import java.text.DateFormat;
import java.util.Date;

public class MtpImage extends MediaItem {
    private static final String TAG = "MtpImage";

    private final int mDeviceId;
    private int mObjectId;
    private int mObjectSize;
    private long mDateTaken;
    private String mFileName;
    private final ThreadPool mThreadPool;
    private final MtpContext mMtpContext;
    private final MtpObjectInfo mObjInfo;
    private final int mImageWidth;
    private final int mImageHeight;

    MtpImage(Path path, GalleryApp application, int deviceId,
            MtpObjectInfo objInfo, MtpContext mtpContext) {
        super(path, nextVersionNumber());
        mDeviceId = deviceId;
        mObjInfo = objInfo;
        mObjectId = objInfo.getObjectHandle();
        mObjectSize = objInfo.getCompressedSize();
        mDateTaken = objInfo.getDateCreated();
        mFileName = objInfo.getName();
        mImageWidth = objInfo.getImagePixWidth();
        mImageHeight = objInfo.getImagePixHeight();
        mThreadPool = application.getThreadPool();
        mMtpContext = mtpContext;
    }

    MtpImage(Path path, GalleryApp app, int deviceId, int objectId, MtpContext mtpContext) {
        this(path, app, deviceId, MtpDevice.getObjectInfo(mtpContext, deviceId, objectId),
                mtpContext);
    }

    @Override
    public long getDateInMs() {
        return mDateTaken;
    }

    @Override
    public Job<Bitmap> requestImage(int type) {
        return new Job<Bitmap>() {
            public Bitmap run(JobContext jc) {
                GetThumbnailBytes job = new GetThumbnailBytes();
                byte[] thumbnail = mThreadPool.submit(job).get();
                if (thumbnail == null) {
                    Log.w(TAG, "decoding thumbnail failed");
                    return null;
                }
                return DecodeUtils.requestDecode(jc, thumbnail, null);
            }
        };
    }

    @Override
    public Job<BitmapRegionDecoder> requestLargeImage() {
        return new Job<BitmapRegionDecoder>() {
            public BitmapRegionDecoder run(JobContext jc) {
                byte[] bytes = mMtpContext.getMtpClient().getObject(
                        UsbDevice.getDeviceName(mDeviceId), mObjectId, mObjectSize);
                return DecodeUtils.requestCreateBitmapRegionDecoder(
                        jc, bytes, 0, bytes.length, false);
            }
        };
    }

    public byte[] getImageData() {
        return mMtpContext.getMtpClient().getObject(
                UsbDevice.getDeviceName(mDeviceId), mObjectId, mObjectSize);
    }

    @Override
    public boolean Import() {
        return mMtpContext.copyFile(UsbDevice.getDeviceName(mDeviceId), mObjInfo);
    }

    @Override
    public int getSupportedOperations() {
        return SUPPORT_FULL_IMAGE | SUPPORT_IMPORT;
    }

    private class GetThumbnailBytes implements Job<byte[]> {
        public byte[] run(JobContext jc) {
            return mMtpContext.getMtpClient().getThumbnail(
                    UsbDevice.getDeviceName(mDeviceId), mObjectId);
        }
    }

    public void updateContent(MtpObjectInfo info) {
        if (mObjectId != info.getObjectHandle() || mDateTaken != info.getDateCreated()) {
            mObjectId = info.getObjectHandle();
            mDateTaken = info.getDateCreated();
            mDataVersion = nextVersionNumber();
        }
    }

    @Override
    public String getMimeType() {
        // Currently only JPEG is supported in MTP.
        return "image/jpeg";
    }

    @Override
    public int getMediaType() {
        return MEDIA_TYPE_IMAGE;
    }

    @Override
    public long getSize() {
        return mObjectSize;
    }

    @Override
    public Uri getContentUri() {
        return GalleryProvider.BASE_URI.buildUpon()
                .appendEncodedPath(mPath.toString().substring(1))
                .build();
    }

    @Override
    public MediaDetails getDetails() {
        MediaDetails details = super.getDetails();
        DateFormat formater = DateFormat.getDateTimeInstance();
        details.addDetail(MediaDetails.INDEX_TITLE, mFileName);
        details.addDetail(MediaDetails.INDEX_DATETIME, formater.format(new Date(mDateTaken)));
        details.addDetail(MediaDetails.INDEX_WIDTH, mImageWidth);
        details.addDetail(MediaDetails.INDEX_HEIGHT, mImageHeight);
        details.addDetail(MediaDetails.INDEX_SIZE, Long.valueOf(mObjectSize));
        return details;
    }

}