use of android.mtp.MtpObjectInfo in project platform_frameworks_base by android.
the class ObjectBrowser method onListItemClick.
@Override
protected void onListItemClick(ListView l, View v, int position, long id) {
MtpObjectInfo info = mObjectList.get(position);
Intent intent;
if (info.getFormat() == MtpConstants.FORMAT_ASSOCIATION) {
intent = new Intent(this, ObjectBrowser.class);
} else {
intent = new Intent(this, ObjectViewer.class);
}
intent.putExtra("device", mDeviceName);
intent.putExtra("storage", mStorageID);
intent.putExtra("object", info.getObjectHandle());
startActivity(intent);
}
use of android.mtp.MtpObjectInfo in project platform_frameworks_base by android.
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.");
}
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(parentId.mStorageId).setParent(parentId.mObjectHandle).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);
}
}
use of android.mtp.MtpObjectInfo in project platform_frameworks_base by android.
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 platform_frameworks_base by android.
the class MtpDocumentsProviderTest method testOpenDocument_shortBytes.
public void testOpenDocument_shortBytes() throws Exception {
mMtpManager = new TestMtpManager(getContext()) {
@Override
MtpObjectInfo getObjectInfo(int deviceId, int objectHandle) throws IOException {
if (objectHandle == 1) {
return new MtpObjectInfo.Builder(super.getObjectInfo(deviceId, objectHandle)).setObjectHandle(1).setCompressedSize(1024 * 1024).build();
}
return super.getObjectInfo(deviceId, objectHandle);
}
};
setupProvider(MtpDatabaseConstants.FLAG_DATABASE_IN_MEMORY);
setupRoots(0, new MtpRoot[] { new MtpRoot(0, 0, "Storage", 0, 0, "") });
final byte[] bytes = "Hello world".getBytes();
setupDocuments(0, 0, MtpManager.OBJECT_HANDLE_ROOT_CHILDREN, "1", new MtpObjectInfo[] { new MtpObjectInfo.Builder().setName("test.txt").setObjectHandle(1).setCompressedSize(bytes.length).setParent(-1).build() });
mMtpManager.setImportFileBytes(0, 1, bytes);
try (final ParcelFileDescriptor fd = mProvider.openDocument("3", "r", null)) {
final byte[] readBytes = new byte[1024 * 1024];
assertEquals(11, Os.read(fd.getFileDescriptor(), readBytes, 0, readBytes.length));
}
}
use of android.mtp.MtpObjectInfo in project android_frameworks_base by AOSPA.
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);
}
}
}
}
}
Aggregations