summaryrefslogtreecommitdiffstats
path: root/media/mtp/MtpCursor.cpp
blob: 35d90dcbfd96af812a0404bd33c40f3c0c14502b (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
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
/*
 * 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.
 */

#define LOG_TAG "MtpCursor"

#include "MtpDebug.h"
#include "MtpClient.h"
#include "MtpCursor.h"
#include "MtpDevice.h"
#include "MtpDeviceInfo.h"
#include "MtpObjectInfo.h"
#include "MtpStorageInfo.h"


#include "binder/CursorWindow.h"

namespace android {

/* Device Column IDs */
/* These must match the values in MtpCursor.java */
#define DEVICE_ROW_ID           1
#define DEVICE_MANUFACTURER     2
#define DEVICE_MODEL            3

/* Storage Column IDs */
/* These must match the values in MtpCursor.java */
#define STORAGE_ROW_ID          101
#define STORAGE_IDENTIFIER      102
#define STORAGE_DESCRIPTION     103

/* Object Column IDs */
/* These must match the values in MtpCursor.java */
#define OBJECT_ROW_ID               201
#define OBJECT_STORAGE_ID           202
#define OBJECT_FORMAT               203
#define OBJECT_PROTECTION_STATUS    204
#define OBJECT_SIZE                 205
#define OBJECT_THUMB_FORMAT         206
#define OBJECT_THUMB_SIZE           207
#define OBJECT_THUMB_WIDTH          208
#define OBJECT_THUMB_HEIGHT         209
#define OBJECT_IMAGE_WIDTH          210
#define OBJECT_IMAGE_HEIGHT         211
#define OBJECT_IMAGE_DEPTH          212
#define OBJECT_PARENT               213
#define OBJECT_ASSOCIATION_TYPE     214
#define OBJECT_ASSOCIATION_DESC     215
#define OBJECT_SEQUENCE_NUMBER      216
#define OBJECT_NAME                 217
#define OBJECT_DATE_CREATED         218
#define OBJECT_DATE_MODIFIED        219
#define OBJECT_KEYWORDS             220
#define OBJECT_THUMB                221

MtpCursor::MtpCursor(MtpClient* client, int queryType, int deviceID,
                MtpStorageID storageID, MtpObjectHandle objectID,
                int columnCount, int* columns)
        :   mClient(client),
            mQueryType(queryType),
            mDeviceID(deviceID),
            mStorageID(storageID),
            mQbjectID(objectID),
            mColumnCount(columnCount),
            mColumns(NULL)
{
    if (columns) {
        mColumns = new int[columnCount];
        memcpy(mColumns, columns, columnCount * sizeof(int));
    }
}

MtpCursor::~MtpCursor() {
    delete[] mColumns;
}

int MtpCursor::fillWindow(CursorWindow* window, int startPos) {
    LOGD("MtpCursor::fillWindow mQueryType: %d\n", mQueryType);

    switch (mQueryType) {
        case DEVICE:
            return fillDevices(window, startPos);
        case DEVICE_ID:
            return fillDevice(window, startPos);
        case STORAGE:
            return fillStorages(window, startPos);
        case STORAGE_ID:
            return fillStorage(window, startPos);
        case OBJECT:
            return fillObjects(window, 0, startPos);
        case OBJECT_ID:
            return fillObject(window, startPos);
        case STORAGE_CHILDREN:
            return fillObjects(window, -1, startPos);
        case OBJECT_CHILDREN:
            return fillObjects(window, mQbjectID, startPos);
        default:
            LOGE("MtpCursor::fillWindow: unknown query type %d\n", mQueryType);
            return 0;
    }
}

int MtpCursor::fillDevices(CursorWindow* window, int startPos) {
    int count = 0;
    MtpDeviceList& deviceList = mClient->getDeviceList();
    for (int i = 0; i < deviceList.size(); i++) {
        MtpDevice* device = deviceList[i];
        if (fillDevice(window, device, startPos)) {
            count++;
            startPos++;
        } else {
            break;
        }
    }
    return count;
}

int MtpCursor::fillDevice(CursorWindow* window, int startPos) {
    MtpDevice* device = mClient->getDevice(mDeviceID);
    if (device && fillDevice(window, device, startPos))
        return 1;
    else
        return 0;
}

int MtpCursor::fillStorages(CursorWindow* window, int startPos) {
    int count = 0;
    MtpDevice* device = mClient->getDevice(mDeviceID);
    if (!device)
        return 0;
    MtpStorageIDList* storageIDs = device->getStorageIDs();
    if (!storageIDs)
        return 0;

    for (int i = 0; i < storageIDs->size(); i++) {
        MtpStorageID storageID = (*storageIDs)[i];
        if (fillStorage(window, device, storageID, startPos)) {
            count++;
            startPos++;
        } else {
            break;
        }
    }
    delete storageIDs;
    return count;
}

int MtpCursor::fillStorage(CursorWindow* window, int startPos) {
    MtpDevice* device = mClient->getDevice(mDeviceID);
    if (device && fillStorage(window, device, mStorageID, startPos))
        return 1;
    else
        return 0;
}

int MtpCursor::fillObjects(CursorWindow* window, int parent, int startPos) {
    int count = 0;
    MtpDevice* device = mClient->getDevice(mDeviceID);
    if (!device)
        return 0;
    MtpObjectHandleList* handles = device->getObjectHandles(mStorageID, 0, parent);
    if (!handles)
        return 0;

    for (int i = 0; i < handles->size(); i++) {
        MtpObjectHandle handle = (*handles)[i];
        if (fillObject(window, device, handle, startPos)) {
            count++;
            startPos++;
        } else {
            break;
        }
    }
    delete handles;
    return count;
}

int MtpCursor::fillObject(CursorWindow* window, int startPos) {
    MtpDevice* device = mClient->getDevice(mDeviceID);
    if (device && fillObject(window, device, mQbjectID, startPos))
        return 1;
    else
        return 0;
}

bool MtpCursor::fillDevice(CursorWindow* window, MtpDevice* device, int row) {
    MtpDeviceInfo* deviceInfo = device->getDeviceInfo();
    if (!deviceInfo)
        return false;
    if (!prepareRow(window))
        return false;

    for (int i = 0; i < mColumnCount; i++) {
        switch (mColumns[i]) {
            case DEVICE_ROW_ID:
                if (!putLong(window, device->getID(), row, i))
                    return false;
                 break;
            case DEVICE_MANUFACTURER:
                if (!putString(window, deviceInfo->mManufacturer, row, i))
                    return false;
                 break;
            case DEVICE_MODEL:
                if (!putString(window, deviceInfo->mModel, row, i))
                    return false;
                 break;
            default:
                LOGE("fillDevice: unknown column %d\n", mColumns[i]);
                return false;
        }
    }

    return true;
}

bool MtpCursor::fillStorage(CursorWindow* window, MtpDevice* device,
        MtpStorageID storageID, int row) {

LOGD("fillStorage %d\n", storageID);

    MtpStorageInfo* storageInfo = device->getStorageInfo(storageID);
    if (!storageInfo)
        return false;
    if (!prepareRow(window)) {
        delete storageInfo;
        return false;
    }

    const char* text;
    for (int i = 0; i < mColumnCount; i++) {
        switch (mColumns[i]) {
            case STORAGE_ROW_ID:
                if (!putLong(window, storageID, row, i))
                    goto fail;
                 break;
            case STORAGE_IDENTIFIER:
                text = storageInfo->mVolumeIdentifier;
                if (!text || !text[0])
                    text = "Camera Storage";
                if (!putString(window, text, row, i))
                    goto fail;
                 break;
            case STORAGE_DESCRIPTION:
                text = storageInfo->mStorageDescription;
                if (!text || !text[0])
                    text = "Storage Description";
                if (!putString(window, text, row, i))
                    goto fail;
                 break;
            default:
                LOGE("fillStorage: unknown column %d\n", mColumns[i]);
                goto fail;
        }
    }

    delete storageInfo;
    return true;

fail:
    delete storageInfo;
    return false;
}

bool MtpCursor::fillObject(CursorWindow* window, MtpDevice* device,
        MtpObjectHandle objectID, int row) {

    MtpObjectInfo* objectInfo = device->getObjectInfo(objectID);
    if (!objectInfo)
        return false;
    // objectInfo->print();
    if (!prepareRow(window)) {
        delete objectInfo;
        return false;
    }

    for (int i = 0; i < mColumnCount; i++) {
        switch (mColumns[i]) {
            case OBJECT_ROW_ID:
                if (!putLong(window, objectID, row, i))
                    goto fail;
                 break;
            case OBJECT_STORAGE_ID:
                if (!putLong(window, objectInfo->mStorageID, row, i))
                    goto fail;
                 break;
            case OBJECT_FORMAT:
                if (!putLong(window, objectInfo->mFormat, row, i))
                    goto fail;
                 break;
            case OBJECT_PROTECTION_STATUS:
                if (!putLong(window, objectInfo->mProtectionStatus, row, i))
                    goto fail;
                 break;
            case OBJECT_SIZE:
                if (!putLong(window, objectInfo->mCompressedSize, row, i))
                    goto fail;
                 break;
            case OBJECT_THUMB_FORMAT:
                if (!putLong(window, objectInfo->mThumbFormat, row, i))
                    goto fail;
                 break;
            case OBJECT_THUMB_SIZE:
                if (!putLong(window, objectInfo->mThumbCompressedSize, row, i))
                    goto fail;
                 break;
            case OBJECT_THUMB_WIDTH:
                if (!putLong(window, objectInfo->mThumbPixWidth, row, i))
                    goto fail;
                 break;
            case OBJECT_THUMB_HEIGHT:
                if (!putLong(window, objectInfo->mThumbPixHeight, row, i))
                    goto fail;
                 break;
            case OBJECT_IMAGE_WIDTH:
                if (!putLong(window, objectInfo->mImagePixWidth, row, i))
                    goto fail;
                 break;
            case OBJECT_IMAGE_HEIGHT:
                if (!putLong(window, objectInfo->mImagePixHeight, row, i))
                    goto fail;
                 break;
            case OBJECT_IMAGE_DEPTH:
                if (!putLong(window, objectInfo->mImagePixDepth, row, i))
                    goto fail;
                 break;
            case OBJECT_PARENT:
                if (!putLong(window, objectInfo->mParent, row, i))
                    goto fail;
                 break;
            case OBJECT_ASSOCIATION_TYPE:
                if (!putLong(window, objectInfo->mAssociationType, row, i))
                    goto fail;
                 break;
            case OBJECT_ASSOCIATION_DESC:
                if (!putLong(window, objectInfo->mAssociationDesc, row, i))
                    goto fail;
                 break;
            case OBJECT_SEQUENCE_NUMBER:
                if (!putLong(window, objectInfo->mSequenceNumber, row, i))
                    goto fail;
                 break;
            case OBJECT_NAME:
                if (!putString(window, objectInfo->mName, row, i))
                    goto fail;
                 break;
            case OBJECT_DATE_CREATED:
                if (!putLong(window, objectInfo->mDateCreated, row, i))
                    goto fail;
                 break;
            case OBJECT_DATE_MODIFIED:
                if (!putLong(window, objectInfo->mDateModified, row, i))
                    goto fail;
                 break;
            case OBJECT_KEYWORDS:
                if (!putString(window, objectInfo->mKeywords, row, i))
                    goto fail;
                 break;
            case OBJECT_THUMB:
                if (!putThumbnail(window, objectID, objectInfo->mFormat, row, i))
                    goto fail;
                break;
            default:
                LOGE("fillObject: unknown column %d\n", mColumns[i]);
                goto fail;
        }
    }

    delete objectInfo;
    return true;

fail:
    delete objectInfo;
    return false;
}

bool MtpCursor::prepareRow(CursorWindow* window) {
    if (!window->setNumColumns(mColumnCount)) {
        LOGE("Failed to change column count from %d to %d", window->getNumColumns(), mColumnCount);
        return false;
    }
    field_slot_t * fieldDir = window->allocRow();
    if (!fieldDir) {
        LOGE("Failed allocating fieldDir");
        return false;
    }
    return true;
}


bool MtpCursor::putLong(CursorWindow* window, int64_t value, int row, int column) {
    if (!window->putLong(row, column, value)) {
        window->freeLastRow();
        LOGE("Failed allocating space for a long in column %d", column);
        return false;
    }
    return true;
}

bool MtpCursor::putString(CursorWindow* window, const char* text, int row, int column) {
    int size = strlen(text) + 1;
    int offset = window->alloc(size);
    if (!offset) {
        window->freeLastRow();
        LOGE("Failed allocating %u bytes for text/blob %s", size, text);
        return false;
    }
    window->copyIn(offset, (const uint8_t*)text, size);

    // This must be updated after the call to alloc(), since that
    // may move the field around in the window
    field_slot_t * fieldSlot = window->getFieldSlot(row, column);
    fieldSlot->type = FIELD_TYPE_STRING;
    fieldSlot->data.buffer.offset = offset;
    fieldSlot->data.buffer.size = size;
    return true;
}

bool MtpCursor::putThumbnail(CursorWindow* window, MtpObjectHandle objectID,
                            MtpObjectFormat format, int row, int column) {
    MtpDevice* device = mClient->getDevice(mDeviceID);
    void* thumbnail;
    int size, offset;
    if (format == MTP_FORMAT_ASSOCIATION) {
        thumbnail = NULL;
        size = offset = 0;
    } else {
        thumbnail = device->getThumbnail(objectID, size);

        LOGV("putThumbnail: %p, size: %d\n", thumbnail, size);
        offset = window->alloc(size);
        if (!offset) {
            window->freeLastRow();
            LOGE("Failed allocating %u bytes for thumbnail", size);
            return false;
        }
    }
    if (thumbnail)
        window->copyIn(offset, (const uint8_t*)thumbnail, size);

    // This must be updated after the call to alloc(), since that
    // may move the field around in the window
    field_slot_t * fieldSlot = window->getFieldSlot(row, column);
    fieldSlot->type = FIELD_TYPE_BLOB;
    fieldSlot->data.buffer.offset = offset;
    fieldSlot->data.buffer.size = size;
    return true;
}

} // namespace android