use of android.mtp.MtpObjectInfo in project android_frameworks_base by DirtyUnicorns.
the class DocumentLoaderTest method testError_GetObjectInfo.
public void testError_GetObjectInfo() throws Exception {
mManager = new BlockableTestMtpManager(getContext()) {
@Override
MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
if (objectHandle == DocumentLoader.NUM_INITIAL_ENTRIES) {
throw new IOException();
} else {
return super.getObjectInfo(deviceId, objectHandle);
}
}
};
setUpLoader();
setUpDocument(mManager, DocumentLoader.NUM_INITIAL_ENTRIES);
try (final Cursor cursor = mLoader.queryChildDocuments(MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier)) {
// Even if MtpManager returns an error for a document, loading must complete.
assertFalse(cursor.getExtras().getBoolean(DocumentsContract.EXTRA_LOADING));
}
}
use of android.mtp.MtpObjectInfo in project android_frameworks_base by DirtyUnicorns.
the class ObjectViewer method onResume.
@Override
protected void onResume() {
super.onResume();
MtpObjectInfo info = mClient.getObjectInfo(mDeviceName, mObjectID);
if (info != null) {
TextView view = (TextView) findViewById(R.id.name);
mFileName = info.getName();
view.setText(mFileName);
view = (TextView) findViewById(R.id.format);
view.setText(Integer.toHexString(info.getFormat()).toUpperCase(Locale.ROOT));
view = (TextView) findViewById(R.id.size);
view.setText(Long.toString(info.getCompressedSize()));
view = (TextView) findViewById(R.id.thumb_width);
view.setText(Long.toString(info.getThumbPixWidth()));
view = (TextView) findViewById(R.id.thumb_height);
view.setText(Long.toString(info.getThumbPixHeight()));
view = (TextView) findViewById(R.id.thumb_size);
view.setText(Long.toString(info.getThumbCompressedSize()));
view = (TextView) findViewById(R.id.width);
view.setText(Long.toString(info.getImagePixWidth()));
view = (TextView) findViewById(R.id.height);
view.setText(Long.toString(info.getImagePixHeight()));
view = (TextView) findViewById(R.id.depth);
view.setText(Long.toString(info.getImagePixDepth()));
view = (TextView) findViewById(R.id.sequence);
view.setText(Long.toString(info.getSequenceNumber()));
view = (TextView) findViewById(R.id.created);
Date date = new Date(info.getDateCreated() * 1000);
view.setText(date.toString());
view = (TextView) findViewById(R.id.modified);
date = new Date(info.getDateModified() * 1000);
view.setText(date.toString());
view = (TextView) findViewById(R.id.keywords);
view.setText(info.getKeywords());
int thumbFormat = info.getThumbFormat();
if (thumbFormat == MtpConstants.FORMAT_EXIF_JPEG || thumbFormat == MtpConstants.FORMAT_JFIF) {
byte[] thumbnail = mClient.getThumbnail(mDeviceName, info.getObjectHandle());
if (thumbnail != null) {
Bitmap bitmap = BitmapFactory.decodeByteArray(thumbnail, 0, thumbnail.length);
if (bitmap != null) {
ImageView thumbView = (ImageView) findViewById(R.id.thumbnail);
thumbView.setImageBitmap(bitmap);
}
}
}
}
}
use of android.mtp.MtpObjectInfo in project android_frameworks_base by crdroidandroid.
the class DocumentLoaderTest method testError_GetObjectInfo.
public void testError_GetObjectInfo() throws Exception {
mManager = new BlockableTestMtpManager(getContext()) {
@Override
MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
if (objectHandle == DocumentLoader.NUM_INITIAL_ENTRIES) {
throw new IOException();
} else {
return super.getObjectInfo(deviceId, objectHandle);
}
}
};
setUpLoader();
setUpDocument(mManager, DocumentLoader.NUM_INITIAL_ENTRIES);
try (final Cursor cursor = mLoader.queryChildDocuments(MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION, mParentIdentifier)) {
// Even if MtpManager returns an error for a document, loading must complete.
assertFalse(cursor.getExtras().getBoolean(DocumentsContract.EXTRA_LOADING));
}
}
use of android.mtp.MtpObjectInfo in project android_frameworks_base by crdroidandroid.
the class MtpDocumentsProviderTest method setupDocuments.
private String[] setupDocuments(int deviceId, int storageId, int parentHandle, String parentDocumentId, MtpObjectInfo[] objects) throws FileNotFoundException {
final int[] handles = new int[objects.length];
int i = 0;
for (final MtpObjectInfo info : objects) {
handles[i] = info.getObjectHandle();
mMtpManager.setObjectInfo(deviceId, info);
}
mMtpManager.setObjectHandles(deviceId, storageId, parentHandle, handles);
return getStrings(mProvider.queryChildDocuments(parentDocumentId, strings(DocumentsContract.Document.COLUMN_DOCUMENT_ID), null));
}
use of android.mtp.MtpObjectInfo in project android_frameworks_base by crdroidandroid.
the class MtpDocumentsProvider method createDocument.
@Override
public String createDocument(String parentDocumentId, String mimeType, String displayName) throws FileNotFoundException {
if (DEBUG) {
Log.d(TAG, "createDocument: " + displayName);
}
final Identifier parentId;
final MtpDeviceRecord record;
final ParcelFileDescriptor[] pipe;
try {
parentId = mDatabase.createIdentifier(parentDocumentId);
openDevice(parentId.mDeviceId);
record = getDeviceToolkit(parentId.mDeviceId).mDeviceRecord;
if (!MtpDeviceRecord.isWritingSupported(record.operationsSupported)) {
throw new UnsupportedOperationException("Writing operation is not supported by the device.");
}
final int parentObjectHandle;
final int storageId;
switch(parentId.mDocumentType) {
case MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE:
final String[] storageDocumentIds = mDatabase.getStorageDocumentIds(parentId.mDocumentId);
if (storageDocumentIds.length == 1) {
final String newDocumentId = createDocument(storageDocumentIds[0], mimeType, displayName);
notifyChildDocumentsChange(parentDocumentId);
return newDocumentId;
} else {
throw new UnsupportedOperationException("Cannot create a file under the device.");
}
case MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE:
storageId = parentId.mStorageId;
parentObjectHandle = -1;
break;
case MtpDatabaseConstants.DOCUMENT_TYPE_OBJECT:
storageId = parentId.mStorageId;
parentObjectHandle = parentId.mObjectHandle;
break;
default:
throw new IllegalArgumentException("Unexpected document type.");
}
pipe = ParcelFileDescriptor.createReliablePipe();
int objectHandle = -1;
MtpObjectInfo info = null;
try {
// 0 bytes for a new document.
pipe[0].close();
final int formatCode = Document.MIME_TYPE_DIR.equals(mimeType) ? MtpConstants.FORMAT_ASSOCIATION : MediaFile.getFormatCode(displayName, mimeType);
info = new MtpObjectInfo.Builder().setStorageId(storageId).setParent(parentObjectHandle).setFormat(formatCode).setName(displayName).build();
final String[] parts = FileUtils.splitFileName(mimeType, displayName);
final String baseName = parts[0];
final String extension = parts[1];
for (int i = 0; i <= 32; i++) {
final MtpObjectInfo infoUniqueName;
if (i == 0) {
infoUniqueName = info;
} else {
String suffixedName = baseName + " (" + i + " )";
if (!extension.isEmpty()) {
suffixedName += "." + extension;
}
infoUniqueName = new MtpObjectInfo.Builder(info).setName(suffixedName).build();
}
try {
objectHandle = mMtpManager.createDocument(parentId.mDeviceId, infoUniqueName, pipe[1]);
break;
} catch (SendObjectInfoFailure exp) {
// This can be caused when we have an existing file with the same name.
continue;
}
}
} finally {
pipe[1].close();
}
if (objectHandle == -1) {
throw new IllegalArgumentException("The file name \"" + displayName + "\" is conflicted with existing files " + "and the provider failed to find unique name.");
}
final MtpObjectInfo infoWithHandle = new MtpObjectInfo.Builder(info).setObjectHandle(objectHandle).build();
final String documentId = mDatabase.putNewDocument(parentId.mDeviceId, parentDocumentId, record.operationsSupported, infoWithHandle, 0l);
getDocumentLoader(parentId).cancelTask(parentId);
notifyChildDocumentsChange(parentDocumentId);
return documentId;
} catch (FileNotFoundException | RuntimeException error) {
Log.e(TAG, "createDocument", error);
throw error;
} catch (IOException error) {
Log.e(TAG, "createDocument", error);
throw new IllegalStateException(error);
}
}
Aggregations