Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 1 | /* |
| 2 | * Copyright (C) 2015 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.mtp; |
| 18 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 19 | import android.content.ContentResolver; |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 20 | import android.content.res.AssetFileDescriptor; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 21 | import android.content.res.Resources; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 22 | import android.database.Cursor; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 23 | import android.database.MatrixCursor; |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 24 | import android.graphics.Point; |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 25 | import android.media.MediaFile; |
| 26 | import android.mtp.MtpConstants; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 27 | import android.mtp.MtpObjectInfo; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 28 | import android.os.Bundle; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 29 | import android.os.CancellationSignal; |
| 30 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 31 | import android.os.storage.StorageManager; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 32 | import android.provider.DocumentsContract.Document; |
| 33 | import android.provider.DocumentsContract.Root; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 34 | import android.provider.DocumentsContract; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 35 | import android.provider.DocumentsProvider; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 36 | import android.util.Log; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 37 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 38 | import com.android.internal.annotations.GuardedBy; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 39 | import com.android.internal.annotations.VisibleForTesting; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 40 | import com.android.mtp.exceptions.BusyDeviceException; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 41 | |
| 42 | import java.io.FileNotFoundException; |
| 43 | import java.io.IOException; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 44 | import java.util.HashMap; |
| 45 | import java.util.Map; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 46 | |
| 47 | /** |
| 48 | * DocumentsProvider for MTP devices. |
| 49 | */ |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 50 | public class MtpDocumentsProvider extends DocumentsProvider { |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 51 | static final String AUTHORITY = "com.android.mtp.documents"; |
| 52 | static final String TAG = "MtpDocumentsProvider"; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 53 | static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 54 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_ICON, |
| 55 | Root.COLUMN_TITLE, Root.COLUMN_DOCUMENT_ID, |
| 56 | Root.COLUMN_AVAILABLE_BYTES, |
| 57 | }; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 58 | static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 59 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, |
| 60 | Document.COLUMN_DISPLAY_NAME, Document.COLUMN_LAST_MODIFIED, |
| 61 | Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 62 | }; |
| 63 | |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 64 | static final boolean DEBUG = false; |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 65 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 66 | private final Object mDeviceListLock = new Object(); |
| 67 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 68 | private static MtpDocumentsProvider sSingleton; |
| 69 | |
| 70 | private MtpManager mMtpManager; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 71 | private ContentResolver mResolver; |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 72 | @GuardedBy("mDeviceListLock") |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 73 | private Map<Integer, DeviceToolkit> mDeviceToolkits; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 74 | private RootScanner mRootScanner; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 75 | private Resources mResources; |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 76 | private MtpDatabase mDatabase; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 77 | private AppFuse mAppFuse; |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 78 | private ServiceIntentSender mIntentSender; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 79 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 80 | /** |
| 81 | * Provides singleton instance to MtpDocumentsService. |
| 82 | */ |
| 83 | static MtpDocumentsProvider getInstance() { |
| 84 | return sSingleton; |
| 85 | } |
| 86 | |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 87 | @Override |
| 88 | public boolean onCreate() { |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 89 | sSingleton = this; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 90 | mResources = getContext().getResources(); |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 91 | mMtpManager = new MtpManager(getContext()); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 92 | mResolver = getContext().getContentResolver(); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 93 | mDeviceToolkits = new HashMap<Integer, DeviceToolkit>(); |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 94 | mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_FILE); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 95 | mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase); |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 96 | mAppFuse = new AppFuse(TAG, new AppFuseCallback()); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 97 | mIntentSender = new ServiceIntentSender(getContext()); |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 98 | // TODO: Mount AppFuse on demands. |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 99 | try { |
| 100 | mAppFuse.mount(getContext().getSystemService(StorageManager.class)); |
| 101 | } catch (IOException e) { |
| 102 | Log.e(TAG, "Failed to start app fuse.", e); |
| 103 | return false; |
| 104 | } |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 105 | resume(); |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 106 | return true; |
| 107 | } |
| 108 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 109 | @VisibleForTesting |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 110 | boolean onCreateForTesting( |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 111 | Resources resources, |
| 112 | MtpManager mtpManager, |
| 113 | ContentResolver resolver, |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 114 | MtpDatabase database, |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 115 | StorageManager storageManager, |
| 116 | ServiceIntentSender intentSender) { |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 117 | mResources = resources; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 118 | mMtpManager = mtpManager; |
| 119 | mResolver = resolver; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 120 | mDeviceToolkits = new HashMap<Integer, DeviceToolkit>(); |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 121 | mDatabase = database; |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 122 | mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 123 | mAppFuse = new AppFuse(TAG, new AppFuseCallback()); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 124 | mIntentSender = intentSender; |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 125 | // TODO: Mount AppFuse on demands. |
| 126 | try { |
| 127 | mAppFuse.mount(storageManager); |
| 128 | } catch (IOException e) { |
| 129 | Log.e(TAG, "Failed to start app fuse.", e); |
| 130 | return false; |
| 131 | } |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 132 | resume(); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 133 | return true; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 134 | } |
| 135 | |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 136 | @Override |
| 137 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 138 | if (projection == null) { |
| 139 | projection = MtpDocumentsProvider.DEFAULT_ROOT_PROJECTION; |
| 140 | } |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 141 | final Cursor cursor = mDatabase.queryRoots(mResources, projection); |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 142 | cursor.setNotificationUri( |
| 143 | mResolver, DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY)); |
| 144 | return cursor; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 145 | } |
| 146 | |
| 147 | @Override |
| 148 | public Cursor queryDocument(String documentId, String[] projection) |
| 149 | throws FileNotFoundException { |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 150 | if (projection == null) { |
| 151 | projection = MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION; |
| 152 | } |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 153 | return mDatabase.queryDocument(documentId, projection); |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 154 | } |
| 155 | |
| 156 | @Override |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 157 | public Cursor queryChildDocuments(String parentDocumentId, |
| 158 | String[] projection, String sortOrder) throws FileNotFoundException { |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 159 | if (DEBUG) { |
| 160 | Log.d(TAG, "queryChildDocuments: " + parentDocumentId); |
| 161 | } |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 162 | if (projection == null) { |
| 163 | projection = MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION; |
| 164 | } |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 165 | Identifier parentIdentifier = mDatabase.createIdentifier(parentDocumentId); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 166 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 167 | openDevice(parentIdentifier.mDeviceId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 168 | if (parentIdentifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE) { |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 169 | final String[] storageDocIds = mDatabase.getStorageDocumentIds(parentDocumentId); |
| 170 | if (storageDocIds.length == 0) { |
| 171 | // Remote device does not provide storages. Maybe it is locked. |
| 172 | return createErrorCursor(projection, R.string.error_locked_device); |
| 173 | } else if (storageDocIds.length > 1) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 174 | // Returns storage list from database. |
| 175 | return mDatabase.queryChildDocuments(projection, parentDocumentId); |
| 176 | } |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 177 | |
| 178 | // Exact one storage is found. Skip storage and returns object in the single |
| 179 | // storage. |
| 180 | parentIdentifier = mDatabase.createIdentifier(storageDocIds[0]); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 181 | } |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 182 | |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 183 | // Returns object list from document loader. |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 184 | return getDocumentLoader(parentIdentifier).queryChildDocuments( |
| 185 | projection, parentIdentifier); |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 186 | } catch (BusyDeviceException exception) { |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 187 | return createErrorCursor(projection, R.string.error_busy_device); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 188 | } catch (IOException exception) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 189 | Log.e(MtpDocumentsProvider.TAG, "queryChildDocuments", exception); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 190 | throw new FileNotFoundException(exception.getMessage()); |
| 191 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 192 | } |
| 193 | |
| 194 | @Override |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 195 | public ParcelFileDescriptor openDocument( |
| 196 | String documentId, String mode, CancellationSignal signal) |
| 197 | throws FileNotFoundException { |
Daichi Hirono | 6213cef | 2016-02-05 17:21:13 +0900 | [diff] [blame] | 198 | if (DEBUG) { |
| 199 | Log.d(TAG, "openDocument: " + documentId); |
| 200 | } |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 201 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 202 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 203 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 204 | final MtpDeviceRecord device = getDeviceToolkit(identifier.mDeviceId).mDeviceRecord; |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 205 | switch (mode) { |
| 206 | case "r": |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 207 | final long fileSize = getFileSize(documentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 208 | // MTP getPartialObject operation does not support files that are larger than |
| 209 | // 4GB. Fallback to non-seekable file descriptor. |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 210 | // TODO: Use getPartialObject64 for MTP devices that support Android vendor |
| 211 | // extension. |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 212 | if (MtpDeviceRecord.isPartialReadSupported( |
| 213 | device.operationsSupported, fileSize)) { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 214 | return mAppFuse.openFile(Integer.parseInt(documentId)); |
| 215 | } else { |
| 216 | return getPipeManager(identifier).readDocument(mMtpManager, identifier); |
| 217 | } |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 218 | case "w": |
Tomasz Mikolajewski | 81d7474 | 2015-09-01 13:45:33 +0900 | [diff] [blame] | 219 | // TODO: Clear the parent document loader task (if exists) and call notify |
| 220 | // when writing is completed. |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 221 | if (MtpDeviceRecord.isWritingSupported(device.operationsSupported)) { |
| 222 | return getPipeManager(identifier).writeDocument( |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 223 | getContext(), mMtpManager, identifier, device.operationsSupported); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 224 | } else { |
| 225 | throw new UnsupportedOperationException( |
| 226 | "The device does not support writing operation."); |
| 227 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 228 | case "rw": |
| 229 | // TODO: Add support for "rw" mode. |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 230 | throw new UnsupportedOperationException( |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 231 | "The provider does not support 'rw' mode."); |
| 232 | default: |
| 233 | throw new IllegalArgumentException("Unknown mode for openDocument: " + mode); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 234 | } |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 235 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 236 | Log.e(MtpDocumentsProvider.TAG, "openDocument", error); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 237 | throw new FileNotFoundException(error.getMessage()); |
| 238 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 239 | } |
| 240 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 241 | @Override |
| 242 | public AssetFileDescriptor openDocumentThumbnail( |
| 243 | String documentId, |
| 244 | Point sizeHint, |
| 245 | CancellationSignal signal) throws FileNotFoundException { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 246 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 247 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 248 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 249 | return new AssetFileDescriptor( |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 250 | getPipeManager(identifier).readThumbnail(mMtpManager, identifier), |
Daichi Hirono | 573c1fb | 2015-08-11 19:31:30 +0900 | [diff] [blame] | 251 | 0, // Start offset. |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 252 | AssetFileDescriptor.UNKNOWN_LENGTH); |
| 253 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 254 | Log.e(MtpDocumentsProvider.TAG, "openDocumentThumbnail", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 255 | throw new FileNotFoundException(error.getMessage()); |
| 256 | } |
| 257 | } |
| 258 | |
| 259 | @Override |
| 260 | public void deleteDocument(String documentId) throws FileNotFoundException { |
| 261 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 262 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 263 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 264 | final Identifier parentIdentifier = mDatabase.getParentIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 265 | mMtpManager.deleteDocument(identifier.mDeviceId, identifier.mObjectHandle); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 266 | mDatabase.deleteDocument(documentId); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 267 | getDocumentLoader(parentIdentifier).clearTask(parentIdentifier); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 268 | notifyChildDocumentsChange(parentIdentifier.mDocumentId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 269 | if (parentIdentifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE) { |
| 270 | // If the parent is storage, the object might be appeared as child of device because |
| 271 | // we skip storage when the device has only one storage. |
| 272 | final Identifier deviceIdentifier = mDatabase.getParentIdentifier( |
| 273 | parentIdentifier.mDocumentId); |
| 274 | notifyChildDocumentsChange(deviceIdentifier.mDocumentId); |
| 275 | } |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 276 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 277 | Log.e(MtpDocumentsProvider.TAG, "deleteDocument", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 278 | throw new FileNotFoundException(error.getMessage()); |
| 279 | } |
| 280 | } |
| 281 | |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 282 | @Override |
| 283 | public void onTrimMemory(int level) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 284 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 285 | for (final DeviceToolkit toolkit : mDeviceToolkits.values()) { |
| 286 | toolkit.mDocumentLoader.clearCompletedTasks(); |
| 287 | } |
| 288 | } |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 289 | } |
| 290 | |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 291 | @Override |
| 292 | public String createDocument(String parentDocumentId, String mimeType, String displayName) |
| 293 | throws FileNotFoundException { |
Daichi Hirono | 6213cef | 2016-02-05 17:21:13 +0900 | [diff] [blame] | 294 | if (DEBUG) { |
| 295 | Log.d(TAG, "createDocument: " + displayName); |
| 296 | } |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 297 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 298 | final Identifier parentId = mDatabase.createIdentifier(parentDocumentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 299 | openDevice(parentId.mDeviceId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 300 | final MtpDeviceRecord record = getDeviceToolkit(parentId.mDeviceId).mDeviceRecord; |
| 301 | if (!MtpDeviceRecord.isWritingSupported(record.operationsSupported)) { |
| 302 | throw new UnsupportedOperationException(); |
| 303 | } |
Tomasz Mikolajewski | df54417 | 2015-08-31 10:59:43 +0900 | [diff] [blame] | 304 | final ParcelFileDescriptor pipe[] = ParcelFileDescriptor.createReliablePipe(); |
| 305 | pipe[0].close(); // 0 bytes for a new document. |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 306 | final int formatCode = Document.MIME_TYPE_DIR.equals(mimeType) ? |
| 307 | MtpConstants.FORMAT_ASSOCIATION : |
| 308 | MediaFile.getFormatCode(displayName, mimeType); |
| 309 | final MtpObjectInfo info = new MtpObjectInfo.Builder() |
| 310 | .setStorageId(parentId.mStorageId) |
| 311 | .setParent(parentId.mObjectHandle) |
| 312 | .setFormat(formatCode) |
| 313 | .setName(displayName) |
| 314 | .build(); |
| 315 | final int objectHandle = mMtpManager.createDocument(parentId.mDeviceId, info, pipe[1]); |
| 316 | final MtpObjectInfo infoWithHandle = |
| 317 | new MtpObjectInfo.Builder(info).setObjectHandle(objectHandle).build(); |
| 318 | final String documentId = mDatabase.putNewDocument( |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 319 | parentId.mDeviceId, parentDocumentId, record.operationsSupported, |
| 320 | infoWithHandle); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 321 | getDocumentLoader(parentId).clearTask(parentId); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 322 | notifyChildDocumentsChange(parentDocumentId); |
| 323 | return documentId; |
| 324 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 325 | Log.e(TAG, "createDocument", error); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 326 | throw new FileNotFoundException(error.getMessage()); |
| 327 | } |
| 328 | } |
| 329 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 330 | void openDevice(int deviceId) throws IOException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 331 | synchronized (mDeviceListLock) { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 332 | if (mDeviceToolkits.containsKey(deviceId)) { |
| 333 | return; |
| 334 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 335 | if (DEBUG) { |
| 336 | Log.d(TAG, "Open device " + deviceId); |
| 337 | } |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 338 | final MtpDeviceRecord device = mMtpManager.openDevice(deviceId); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 339 | final DeviceToolkit toolkit = |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 340 | new DeviceToolkit(mMtpManager, mResolver, mDatabase, device); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 341 | mDeviceToolkits.put(deviceId, toolkit); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 342 | mIntentSender.sendUpdateNotificationIntent(); |
| 343 | try { |
| 344 | mRootScanner.resume().await(); |
| 345 | } catch (InterruptedException error) { |
| 346 | Log.e(TAG, "openDevice", error); |
| 347 | } |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 348 | // Resume document loader to remap disconnected document ID. Must be invoked after the |
| 349 | // root scanner resumes. |
| 350 | toolkit.mDocumentLoader.resume(); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 351 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 352 | } |
| 353 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 354 | void closeDevice(int deviceId) throws IOException, InterruptedException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 355 | synchronized (mDeviceListLock) { |
| 356 | closeDeviceInternal(deviceId); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 357 | } |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 358 | mRootScanner.resume(); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 359 | mIntentSender.sendUpdateNotificationIntent(); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 360 | } |
| 361 | |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 362 | MtpDeviceRecord[] getOpenedDeviceRecordsCache() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 363 | synchronized (mDeviceListLock) { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 364 | final MtpDeviceRecord[] records = new MtpDeviceRecord[mDeviceToolkits.size()]; |
| 365 | int i = 0; |
| 366 | for (final DeviceToolkit toolkit : mDeviceToolkits.values()) { |
| 367 | records[i] = toolkit.mDeviceRecord; |
| 368 | i++; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 369 | } |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 370 | return records; |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 371 | } |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 372 | } |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 373 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 374 | /** |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 375 | * Obtains document ID for the given device ID. |
| 376 | * @param deviceId |
| 377 | * @return document ID |
| 378 | * @throws FileNotFoundException device ID has not been build. |
| 379 | */ |
| 380 | public String getDeviceDocumentId(int deviceId) throws FileNotFoundException { |
| 381 | return mDatabase.getDeviceDocumentId(deviceId); |
| 382 | } |
| 383 | |
| 384 | /** |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 385 | * Resumes root scanner to handle the update of device list. |
| 386 | */ |
| 387 | void resumeRootScanner() { |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 388 | if (DEBUG) { |
| 389 | Log.d(MtpDocumentsProvider.TAG, "resumeRootScanner"); |
| 390 | } |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 391 | mRootScanner.resume(); |
| 392 | } |
| 393 | |
| 394 | /** |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 395 | * Finalize the content provider for unit tests. |
| 396 | */ |
| 397 | @Override |
| 398 | public void shutdown() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 399 | synchronized (mDeviceListLock) { |
| 400 | try { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 401 | // Copy the opened key set because it will be modified when closing devices. |
| 402 | final Integer[] keySet = |
| 403 | mDeviceToolkits.keySet().toArray(new Integer[mDeviceToolkits.size()]); |
| 404 | for (final int id : keySet) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 405 | closeDeviceInternal(id); |
| 406 | } |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 407 | mRootScanner.pause(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 408 | } catch (InterruptedException|IOException e) { |
| 409 | // It should fail unit tests by throwing runtime exception. |
| 410 | throw new RuntimeException(e); |
| 411 | } finally { |
| 412 | mDatabase.close(); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 413 | mAppFuse.close(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 414 | super.shutdown(); |
| 415 | } |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 416 | } |
| 417 | } |
| 418 | |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 419 | private void notifyChildDocumentsChange(String parentDocumentId) { |
| 420 | mResolver.notifyChange( |
| 421 | DocumentsContract.buildChildDocumentsUri(AUTHORITY, parentDocumentId), |
| 422 | null, |
| 423 | false); |
| 424 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 425 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 426 | /** |
Daichi Hirono | be38848 | 2015-12-14 16:20:14 +0900 | [diff] [blame] | 427 | * Clears MTP identifier in the database. |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 428 | */ |
| 429 | private void resume() { |
| 430 | synchronized (mDeviceListLock) { |
| 431 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 432 | } |
| 433 | } |
| 434 | |
| 435 | private void closeDeviceInternal(int deviceId) throws IOException, InterruptedException { |
| 436 | // TODO: Flush the device before closing (if not closed externally). |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 437 | if (!mDeviceToolkits.containsKey(deviceId)) { |
| 438 | return; |
| 439 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 440 | if (DEBUG) { |
| 441 | Log.d(TAG, "Close device " + deviceId); |
| 442 | } |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 443 | getDeviceToolkit(deviceId).mDocumentLoader.close(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 444 | mDeviceToolkits.remove(deviceId); |
| 445 | mMtpManager.closeDevice(deviceId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 446 | if (mDeviceToolkits.size() == 0) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 447 | mRootScanner.pause(); |
| 448 | } |
| 449 | } |
| 450 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 451 | private DeviceToolkit getDeviceToolkit(int deviceId) throws FileNotFoundException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 452 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 453 | final DeviceToolkit toolkit = mDeviceToolkits.get(deviceId); |
| 454 | if (toolkit == null) { |
| 455 | throw new FileNotFoundException(); |
| 456 | } |
| 457 | return toolkit; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 458 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 459 | } |
| 460 | |
| 461 | private PipeManager getPipeManager(Identifier identifier) throws FileNotFoundException { |
| 462 | return getDeviceToolkit(identifier.mDeviceId).mPipeManager; |
| 463 | } |
| 464 | |
| 465 | private DocumentLoader getDocumentLoader(Identifier identifier) throws FileNotFoundException { |
| 466 | return getDeviceToolkit(identifier.mDeviceId).mDocumentLoader; |
| 467 | } |
| 468 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 469 | private long getFileSize(String documentId) throws FileNotFoundException { |
| 470 | final Cursor cursor = mDatabase.queryDocument( |
| 471 | documentId, |
| 472 | MtpDatabase.strings(Document.COLUMN_SIZE, Document.COLUMN_DISPLAY_NAME)); |
| 473 | try { |
| 474 | if (cursor.moveToNext()) { |
| 475 | return cursor.getLong(0); |
| 476 | } else { |
| 477 | throw new FileNotFoundException(); |
| 478 | } |
| 479 | } finally { |
| 480 | cursor.close(); |
| 481 | } |
| 482 | } |
| 483 | |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 484 | /** |
| 485 | * Creates empty cursor with specific error message. |
| 486 | * |
| 487 | * @param projection Column names. |
| 488 | * @param stringResId String resource ID of error message. |
| 489 | * @return Empty cursor with error message. |
| 490 | */ |
| 491 | private Cursor createErrorCursor(String[] projection, int stringResId) { |
| 492 | final Bundle bundle = new Bundle(); |
| 493 | bundle.putString(DocumentsContract.EXTRA_ERROR, mResources.getString(stringResId)); |
| 494 | final Cursor cursor = new MatrixCursor(projection); |
| 495 | cursor.setExtras(bundle); |
| 496 | return cursor; |
| 497 | } |
| 498 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 499 | private static class DeviceToolkit { |
| 500 | public final PipeManager mPipeManager; |
| 501 | public final DocumentLoader mDocumentLoader; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 502 | public final MtpDeviceRecord mDeviceRecord; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 503 | |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 504 | public DeviceToolkit(MtpManager manager, |
| 505 | ContentResolver resolver, |
| 506 | MtpDatabase database, |
| 507 | MtpDeviceRecord record) { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 508 | mPipeManager = new PipeManager(database); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 509 | mDocumentLoader = new DocumentLoader(record, manager, resolver, database); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 510 | mDeviceRecord = record; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 511 | } |
| 512 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 513 | |
| 514 | private class AppFuseCallback implements AppFuse.Callback { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 515 | @Override |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 516 | public long readObjectBytes( |
| 517 | int inode, long offset, long size, byte[] buffer) throws IOException { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 518 | final Identifier identifier = mDatabase.createIdentifier(Integer.toString(inode)); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 519 | final MtpDeviceRecord record = getDeviceToolkit(identifier.mDeviceId).mDeviceRecord; |
| 520 | if (MtpDeviceRecord.isPartialReadSupported(record.operationsSupported, offset)) { |
| 521 | return mMtpManager.getPartialObject( |
| 522 | identifier.mDeviceId, identifier.mObjectHandle, offset, size, buffer); |
| 523 | } else { |
| 524 | throw new UnsupportedOperationException(); |
| 525 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 526 | } |
| 527 | |
| 528 | @Override |
| 529 | public long getFileSize(int inode) throws FileNotFoundException { |
| 530 | return MtpDocumentsProvider.this.getFileSize(String.valueOf(inode)); |
| 531 | } |
| 532 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 533 | } |