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); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 204 | switch (mode) { |
| 205 | case "r": |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 206 | final long fileSize = getFileSize(documentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 207 | // MTP getPartialObject operation does not support files that are larger than |
| 208 | // 4GB. Fallback to non-seekable file descriptor. |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 209 | // TODO: Use getPartialObject64 for MTP devices that support Android vendor |
| 210 | // extension. |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 211 | if (fileSize <= 0xffffffffl) { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 212 | return mAppFuse.openFile(Integer.parseInt(documentId)); |
| 213 | } else { |
| 214 | return getPipeManager(identifier).readDocument(mMtpManager, identifier); |
| 215 | } |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 216 | case "w": |
Tomasz Mikolajewski | 81d7474 | 2015-09-01 13:45:33 +0900 | [diff] [blame] | 217 | // TODO: Clear the parent document loader task (if exists) and call notify |
| 218 | // when writing is completed. |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 219 | return getPipeManager(identifier).writeDocument( |
| 220 | getContext(), mMtpManager, identifier); |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 221 | case "rw": |
| 222 | // TODO: Add support for "rw" mode. |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 223 | throw new UnsupportedOperationException( |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 224 | "The provider does not support 'rw' mode."); |
| 225 | default: |
| 226 | throw new IllegalArgumentException("Unknown mode for openDocument: " + mode); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 227 | } |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 228 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 229 | Log.e(MtpDocumentsProvider.TAG, "openDocument", error); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 230 | throw new FileNotFoundException(error.getMessage()); |
| 231 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 232 | } |
| 233 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 234 | @Override |
| 235 | public AssetFileDescriptor openDocumentThumbnail( |
| 236 | String documentId, |
| 237 | Point sizeHint, |
| 238 | CancellationSignal signal) throws FileNotFoundException { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 239 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 240 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 241 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 242 | return new AssetFileDescriptor( |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 243 | getPipeManager(identifier).readThumbnail(mMtpManager, identifier), |
Daichi Hirono | 573c1fb | 2015-08-11 19:31:30 +0900 | [diff] [blame] | 244 | 0, // Start offset. |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 245 | AssetFileDescriptor.UNKNOWN_LENGTH); |
| 246 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 247 | Log.e(MtpDocumentsProvider.TAG, "openDocumentThumbnail", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 248 | throw new FileNotFoundException(error.getMessage()); |
| 249 | } |
| 250 | } |
| 251 | |
| 252 | @Override |
| 253 | public void deleteDocument(String documentId) throws FileNotFoundException { |
| 254 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 255 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 256 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 257 | final Identifier parentIdentifier = mDatabase.getParentIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 258 | mMtpManager.deleteDocument(identifier.mDeviceId, identifier.mObjectHandle); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 259 | mDatabase.deleteDocument(documentId); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 260 | getDocumentLoader(parentIdentifier).clearTask(parentIdentifier); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 261 | notifyChildDocumentsChange(parentIdentifier.mDocumentId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 262 | if (parentIdentifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE) { |
| 263 | // If the parent is storage, the object might be appeared as child of device because |
| 264 | // we skip storage when the device has only one storage. |
| 265 | final Identifier deviceIdentifier = mDatabase.getParentIdentifier( |
| 266 | parentIdentifier.mDocumentId); |
| 267 | notifyChildDocumentsChange(deviceIdentifier.mDocumentId); |
| 268 | } |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 269 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 270 | Log.e(MtpDocumentsProvider.TAG, "deleteDocument", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 271 | throw new FileNotFoundException(error.getMessage()); |
| 272 | } |
| 273 | } |
| 274 | |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 275 | @Override |
| 276 | public void onTrimMemory(int level) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 277 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 278 | for (final DeviceToolkit toolkit : mDeviceToolkits.values()) { |
| 279 | toolkit.mDocumentLoader.clearCompletedTasks(); |
| 280 | } |
| 281 | } |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 282 | } |
| 283 | |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 284 | @Override |
| 285 | public String createDocument(String parentDocumentId, String mimeType, String displayName) |
| 286 | throws FileNotFoundException { |
Daichi Hirono | 6213cef | 2016-02-05 17:21:13 +0900 | [diff] [blame] | 287 | if (DEBUG) { |
| 288 | Log.d(TAG, "createDocument: " + displayName); |
| 289 | } |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 290 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 291 | final Identifier parentId = mDatabase.createIdentifier(parentDocumentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 292 | openDevice(parentId.mDeviceId); |
Tomasz Mikolajewski | df54417 | 2015-08-31 10:59:43 +0900 | [diff] [blame] | 293 | final ParcelFileDescriptor pipe[] = ParcelFileDescriptor.createReliablePipe(); |
| 294 | pipe[0].close(); // 0 bytes for a new document. |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 295 | final int formatCode = Document.MIME_TYPE_DIR.equals(mimeType) ? |
| 296 | MtpConstants.FORMAT_ASSOCIATION : |
| 297 | MediaFile.getFormatCode(displayName, mimeType); |
| 298 | final MtpObjectInfo info = new MtpObjectInfo.Builder() |
| 299 | .setStorageId(parentId.mStorageId) |
| 300 | .setParent(parentId.mObjectHandle) |
| 301 | .setFormat(formatCode) |
| 302 | .setName(displayName) |
| 303 | .build(); |
| 304 | final int objectHandle = mMtpManager.createDocument(parentId.mDeviceId, info, pipe[1]); |
| 305 | final MtpObjectInfo infoWithHandle = |
| 306 | new MtpObjectInfo.Builder(info).setObjectHandle(objectHandle).build(); |
| 307 | final String documentId = mDatabase.putNewDocument( |
| 308 | parentId.mDeviceId, parentDocumentId, infoWithHandle); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 309 | getDocumentLoader(parentId).clearTask(parentId); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 310 | notifyChildDocumentsChange(parentDocumentId); |
| 311 | return documentId; |
| 312 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 313 | Log.e(TAG, "createDocument", error); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 314 | throw new FileNotFoundException(error.getMessage()); |
| 315 | } |
| 316 | } |
| 317 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 318 | void openDevice(int deviceId) throws IOException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 319 | synchronized (mDeviceListLock) { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 320 | if (mDeviceToolkits.containsKey(deviceId)) { |
| 321 | return; |
| 322 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 323 | if (DEBUG) { |
| 324 | Log.d(TAG, "Open device " + deviceId); |
| 325 | } |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 326 | mMtpManager.openDevice(deviceId); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 327 | final DeviceToolkit toolkit = |
| 328 | new DeviceToolkit(deviceId, mMtpManager, mResolver, mDatabase); |
| 329 | mDeviceToolkits.put(deviceId, toolkit); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 330 | mIntentSender.sendUpdateNotificationIntent(); |
| 331 | try { |
| 332 | mRootScanner.resume().await(); |
| 333 | } catch (InterruptedException error) { |
| 334 | Log.e(TAG, "openDevice", error); |
| 335 | } |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 336 | // Resume document loader to remap disconnected document ID. Must be invoked after the |
| 337 | // root scanner resumes. |
| 338 | toolkit.mDocumentLoader.resume(); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 339 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 340 | } |
| 341 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 342 | void closeDevice(int deviceId) throws IOException, InterruptedException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 343 | synchronized (mDeviceListLock) { |
| 344 | closeDeviceInternal(deviceId); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 345 | } |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 346 | mRootScanner.resume(); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 347 | mIntentSender.sendUpdateNotificationIntent(); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 348 | } |
| 349 | |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 350 | int[] getOpenedDeviceIds() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 351 | synchronized (mDeviceListLock) { |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 352 | return mMtpManager.getOpenedDeviceIds(); |
| 353 | } |
| 354 | } |
| 355 | |
| 356 | String getDeviceName(int deviceId) throws IOException { |
| 357 | synchronized (mDeviceListLock) { |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 358 | for (final MtpDeviceRecord device : mMtpManager.getDevices()) { |
| 359 | if (device.deviceId == deviceId) { |
| 360 | return device.name; |
| 361 | } |
| 362 | } |
| 363 | throw new IOException("Not found the device: " + Integer.toString(deviceId)); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 364 | } |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 365 | } |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 366 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 367 | /** |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 368 | * Obtains document ID for the given device ID. |
| 369 | * @param deviceId |
| 370 | * @return document ID |
| 371 | * @throws FileNotFoundException device ID has not been build. |
| 372 | */ |
| 373 | public String getDeviceDocumentId(int deviceId) throws FileNotFoundException { |
| 374 | return mDatabase.getDeviceDocumentId(deviceId); |
| 375 | } |
| 376 | |
| 377 | /** |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 378 | * Resumes root scanner to handle the update of device list. |
| 379 | */ |
| 380 | void resumeRootScanner() { |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 381 | if (DEBUG) { |
| 382 | Log.d(MtpDocumentsProvider.TAG, "resumeRootScanner"); |
| 383 | } |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 384 | mRootScanner.resume(); |
| 385 | } |
| 386 | |
| 387 | /** |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 388 | * Finalize the content provider for unit tests. |
| 389 | */ |
| 390 | @Override |
| 391 | public void shutdown() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 392 | synchronized (mDeviceListLock) { |
| 393 | try { |
| 394 | for (final int id : mMtpManager.getOpenedDeviceIds()) { |
| 395 | closeDeviceInternal(id); |
| 396 | } |
| 397 | } catch (InterruptedException|IOException e) { |
| 398 | // It should fail unit tests by throwing runtime exception. |
| 399 | throw new RuntimeException(e); |
| 400 | } finally { |
| 401 | mDatabase.close(); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 402 | mAppFuse.close(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 403 | super.shutdown(); |
| 404 | } |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 405 | } |
| 406 | } |
| 407 | |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 408 | private void notifyChildDocumentsChange(String parentDocumentId) { |
| 409 | mResolver.notifyChange( |
| 410 | DocumentsContract.buildChildDocumentsUri(AUTHORITY, parentDocumentId), |
| 411 | null, |
| 412 | false); |
| 413 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 414 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 415 | /** |
Daichi Hirono | be38848 | 2015-12-14 16:20:14 +0900 | [diff] [blame] | 416 | * Clears MTP identifier in the database. |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 417 | */ |
| 418 | private void resume() { |
| 419 | synchronized (mDeviceListLock) { |
| 420 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 421 | } |
| 422 | } |
| 423 | |
| 424 | private void closeDeviceInternal(int deviceId) throws IOException, InterruptedException { |
| 425 | // TODO: Flush the device before closing (if not closed externally). |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 426 | if (!mDeviceToolkits.containsKey(deviceId)) { |
| 427 | return; |
| 428 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 429 | if (DEBUG) { |
| 430 | Log.d(TAG, "Close device " + deviceId); |
| 431 | } |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 432 | getDeviceToolkit(deviceId).mDocumentLoader.close(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 433 | mDeviceToolkits.remove(deviceId); |
| 434 | mMtpManager.closeDevice(deviceId); |
Daichi Hirono | a57d9ed | 2015-12-07 10:52:42 +0900 | [diff] [blame] | 435 | if (getOpenedDeviceIds().length == 0) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 436 | mRootScanner.pause(); |
| 437 | } |
| 438 | } |
| 439 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 440 | private DeviceToolkit getDeviceToolkit(int deviceId) throws FileNotFoundException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 441 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 442 | final DeviceToolkit toolkit = mDeviceToolkits.get(deviceId); |
| 443 | if (toolkit == null) { |
| 444 | throw new FileNotFoundException(); |
| 445 | } |
| 446 | return toolkit; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 447 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 448 | } |
| 449 | |
| 450 | private PipeManager getPipeManager(Identifier identifier) throws FileNotFoundException { |
| 451 | return getDeviceToolkit(identifier.mDeviceId).mPipeManager; |
| 452 | } |
| 453 | |
| 454 | private DocumentLoader getDocumentLoader(Identifier identifier) throws FileNotFoundException { |
| 455 | return getDeviceToolkit(identifier.mDeviceId).mDocumentLoader; |
| 456 | } |
| 457 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 458 | private long getFileSize(String documentId) throws FileNotFoundException { |
| 459 | final Cursor cursor = mDatabase.queryDocument( |
| 460 | documentId, |
| 461 | MtpDatabase.strings(Document.COLUMN_SIZE, Document.COLUMN_DISPLAY_NAME)); |
| 462 | try { |
| 463 | if (cursor.moveToNext()) { |
| 464 | return cursor.getLong(0); |
| 465 | } else { |
| 466 | throw new FileNotFoundException(); |
| 467 | } |
| 468 | } finally { |
| 469 | cursor.close(); |
| 470 | } |
| 471 | } |
| 472 | |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 473 | /** |
| 474 | * Creates empty cursor with specific error message. |
| 475 | * |
| 476 | * @param projection Column names. |
| 477 | * @param stringResId String resource ID of error message. |
| 478 | * @return Empty cursor with error message. |
| 479 | */ |
| 480 | private Cursor createErrorCursor(String[] projection, int stringResId) { |
| 481 | final Bundle bundle = new Bundle(); |
| 482 | bundle.putString(DocumentsContract.EXTRA_ERROR, mResources.getString(stringResId)); |
| 483 | final Cursor cursor = new MatrixCursor(projection); |
| 484 | cursor.setExtras(bundle); |
| 485 | return cursor; |
| 486 | } |
| 487 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 488 | private static class DeviceToolkit { |
| 489 | public final PipeManager mPipeManager; |
| 490 | public final DocumentLoader mDocumentLoader; |
| 491 | |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 492 | public DeviceToolkit( |
| 493 | int deviceId, MtpManager manager, ContentResolver resolver, MtpDatabase database) { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 494 | mPipeManager = new PipeManager(database); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 495 | mDocumentLoader = new DocumentLoader(deviceId, manager, resolver, database); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 496 | } |
| 497 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 498 | |
| 499 | private class AppFuseCallback implements AppFuse.Callback { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 500 | @Override |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 501 | public long readObjectBytes( |
| 502 | int inode, long offset, long size, byte[] buffer) throws IOException { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 503 | final Identifier identifier = mDatabase.createIdentifier(Integer.toString(inode)); |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 504 | return mMtpManager.getPartialObject( |
| 505 | identifier.mDeviceId, identifier.mObjectHandle, offset, size, buffer); |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 506 | } |
| 507 | |
| 508 | @Override |
| 509 | public long getFileSize(int inode) throws FileNotFoundException { |
| 510 | return MtpDocumentsProvider.this.getFileSize(String.valueOf(inode)); |
| 511 | } |
| 512 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 513 | } |