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 | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 20 | import android.content.Context; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 21 | import android.content.UriPermission; |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 22 | import android.content.res.AssetFileDescriptor; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 23 | import android.content.res.Resources; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 24 | import android.database.Cursor; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 25 | import android.database.MatrixCursor; |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 26 | import android.database.sqlite.SQLiteDiskIOException; |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 27 | import android.graphics.Point; |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 28 | import android.media.MediaFile; |
| 29 | import android.mtp.MtpConstants; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 30 | import android.mtp.MtpObjectInfo; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 31 | import android.net.Uri; |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 32 | import android.os.Bundle; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 33 | import android.os.CancellationSignal; |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 34 | import android.os.FileUtils; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 35 | import android.os.ParcelFileDescriptor; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 36 | import android.os.storage.StorageManager; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 37 | import android.provider.DocumentsContract.Document; |
| 38 | import android.provider.DocumentsContract.Root; |
Tomasz Mikolajewski | bb430fa | 2015-08-25 18:34:30 +0900 | [diff] [blame] | 39 | import android.provider.DocumentsContract; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 40 | import android.provider.DocumentsProvider; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 41 | import android.provider.Settings; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 42 | import android.system.ErrnoException; |
| 43 | import android.system.Os; |
| 44 | import android.system.OsConstants; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 45 | import android.util.Log; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 46 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 47 | import com.android.internal.annotations.GuardedBy; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 48 | import com.android.internal.annotations.VisibleForTesting; |
| 49 | |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 50 | import java.io.File; |
| 51 | import java.io.FileDescriptor; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 52 | import java.io.FileNotFoundException; |
| 53 | import java.io.IOException; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 54 | import java.util.HashMap; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 55 | import java.util.List; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 56 | import java.util.Map; |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 57 | import java.util.concurrent.TimeoutException; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 58 | |
| 59 | /** |
| 60 | * DocumentsProvider for MTP devices. |
| 61 | */ |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 62 | public class MtpDocumentsProvider extends DocumentsProvider { |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 63 | static final String AUTHORITY = "com.android.mtp.documents"; |
| 64 | static final String TAG = "MtpDocumentsProvider"; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 65 | static final String[] DEFAULT_ROOT_PROJECTION = new String[] { |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 66 | Root.COLUMN_ROOT_ID, Root.COLUMN_FLAGS, Root.COLUMN_ICON, |
| 67 | Root.COLUMN_TITLE, Root.COLUMN_DOCUMENT_ID, |
| 68 | Root.COLUMN_AVAILABLE_BYTES, |
| 69 | }; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 70 | static final String[] DEFAULT_DOCUMENT_PROJECTION = new String[] { |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 71 | Document.COLUMN_DOCUMENT_ID, Document.COLUMN_MIME_TYPE, |
| 72 | Document.COLUMN_DISPLAY_NAME, Document.COLUMN_LAST_MODIFIED, |
| 73 | Document.COLUMN_FLAGS, Document.COLUMN_SIZE, |
| 74 | }; |
| 75 | |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 76 | static final boolean DEBUG = false; |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 77 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 78 | private final Object mDeviceListLock = new Object(); |
| 79 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 80 | private static MtpDocumentsProvider sSingleton; |
| 81 | |
| 82 | private MtpManager mMtpManager; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 83 | private ContentResolver mResolver; |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 84 | @GuardedBy("mDeviceListLock") |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 85 | private Map<Integer, DeviceToolkit> mDeviceToolkits; |
Daichi Hirono | 8b9024f | 2015-08-12 12:59:09 +0900 | [diff] [blame] | 86 | private RootScanner mRootScanner; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 87 | private Resources mResources; |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 88 | private MtpDatabase mDatabase; |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 89 | private AppFuse mAppFuse; |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 90 | private ServiceIntentSender mIntentSender; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 91 | private Context mContext; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 92 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 93 | /** |
| 94 | * Provides singleton instance to MtpDocumentsService. |
| 95 | */ |
| 96 | static MtpDocumentsProvider getInstance() { |
| 97 | return sSingleton; |
| 98 | } |
| 99 | |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 100 | @Override |
| 101 | public boolean onCreate() { |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 102 | sSingleton = this; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 103 | mContext = getContext(); |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 104 | mResources = getContext().getResources(); |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 105 | mMtpManager = new MtpManager(getContext()); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 106 | mResolver = getContext().getContentResolver(); |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 107 | mDeviceToolkits = new HashMap<Integer, DeviceToolkit>(); |
Daichi Hirono | 47eb192 | 2015-11-16 13:01:31 +0900 | [diff] [blame] | 108 | mDatabase = new MtpDatabase(getContext(), MtpDatabaseConstants.FLAG_DATABASE_IN_FILE); |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 109 | mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase); |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 110 | mAppFuse = new AppFuse(TAG, new AppFuseCallback()); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 111 | mIntentSender = new ServiceIntentSender(getContext()); |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 112 | |
| 113 | // Check boot count and cleans database if it's first time to launch MtpDocumentsProvider |
| 114 | // after booting. |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 115 | try { |
| 116 | final int bootCount = Settings.Global.getInt(mResolver, Settings.Global.BOOT_COUNT, -1); |
| 117 | final int lastBootCount = mDatabase.getLastBootCount(); |
| 118 | if (bootCount != -1 && bootCount != lastBootCount) { |
| 119 | mDatabase.setLastBootCount(bootCount); |
| 120 | final List<UriPermission> permissions = |
| 121 | mResolver.getOutgoingPersistedUriPermissions(); |
| 122 | final Uri[] uris = new Uri[permissions.size()]; |
| 123 | for (int i = 0; i < permissions.size(); i++) { |
| 124 | uris[i] = permissions.get(i).getUri(); |
| 125 | } |
| 126 | mDatabase.cleanDatabase(uris); |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 127 | } |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 128 | } catch (SQLiteDiskIOException error) { |
| 129 | // It can happen due to disk shortage. |
| 130 | Log.e(TAG, "Failed to clean database.", error); |
| 131 | return false; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 132 | } |
| 133 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 134 | // TODO: Mount AppFuse on demands. |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 135 | try { |
| 136 | mAppFuse.mount(getContext().getSystemService(StorageManager.class)); |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 137 | } catch (IOException error) { |
| 138 | Log.e(TAG, "Failed to start app fuse.", error); |
Daichi Hirono | e6054c0 | 2016-01-20 15:36:04 +0900 | [diff] [blame] | 139 | return false; |
| 140 | } |
Daichi Hirono | 5884e1f | 2016-03-16 14:36:27 +0900 | [diff] [blame] | 141 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 142 | resume(); |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 143 | return true; |
| 144 | } |
| 145 | |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 146 | @VisibleForTesting |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 147 | boolean onCreateForTesting( |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 148 | Context context, |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 149 | Resources resources, |
| 150 | MtpManager mtpManager, |
| 151 | ContentResolver resolver, |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 152 | MtpDatabase database, |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 153 | StorageManager storageManager, |
| 154 | ServiceIntentSender intentSender) { |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 155 | mContext = context; |
Daichi Hirono | 17c8d8b | 2015-10-12 11:28:46 -0700 | [diff] [blame] | 156 | mResources = resources; |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 157 | mMtpManager = mtpManager; |
| 158 | mResolver = resolver; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 159 | mDeviceToolkits = new HashMap<Integer, DeviceToolkit>(); |
Daichi Hirono | dc47344 | 2015-11-13 15:42:28 +0900 | [diff] [blame] | 160 | mDatabase = database; |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 161 | mRootScanner = new RootScanner(mResolver, mMtpManager, mDatabase); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 162 | mAppFuse = new AppFuse(TAG, new AppFuseCallback()); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 163 | mIntentSender = intentSender; |
Daichi Hirono | 3bb37e7 | 2016-02-29 15:30:56 +0900 | [diff] [blame] | 164 | |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 165 | // TODO: Mount AppFuse on demands. |
| 166 | try { |
| 167 | mAppFuse.mount(storageManager); |
| 168 | } catch (IOException e) { |
| 169 | Log.e(TAG, "Failed to start app fuse.", e); |
| 170 | return false; |
| 171 | } |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 172 | resume(); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 173 | return true; |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 174 | } |
| 175 | |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 176 | @Override |
| 177 | public Cursor queryRoots(String[] projection) throws FileNotFoundException { |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 178 | if (projection == null) { |
| 179 | projection = MtpDocumentsProvider.DEFAULT_ROOT_PROJECTION; |
| 180 | } |
Daichi Hirono | f83ccbd | 2016-02-04 16:58:55 +0900 | [diff] [blame] | 181 | final Cursor cursor = mDatabase.queryRoots(mResources, projection); |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 182 | cursor.setNotificationUri( |
| 183 | mResolver, DocumentsContract.buildRootsUri(MtpDocumentsProvider.AUTHORITY)); |
| 184 | return cursor; |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 185 | } |
| 186 | |
| 187 | @Override |
| 188 | public Cursor queryDocument(String documentId, String[] projection) |
| 189 | throws FileNotFoundException { |
Daichi Hirono | e5323b7 | 2015-07-29 16:10:47 +0900 | [diff] [blame] | 190 | if (projection == null) { |
| 191 | projection = MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION; |
| 192 | } |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 193 | return mDatabase.queryDocument(documentId, projection); |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 194 | } |
| 195 | |
| 196 | @Override |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 197 | public Cursor queryChildDocuments(String parentDocumentId, |
| 198 | String[] projection, String sortOrder) throws FileNotFoundException { |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 199 | if (DEBUG) { |
| 200 | Log.d(TAG, "queryChildDocuments: " + parentDocumentId); |
| 201 | } |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 202 | if (projection == null) { |
| 203 | projection = MtpDocumentsProvider.DEFAULT_DOCUMENT_PROJECTION; |
| 204 | } |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 205 | Identifier parentIdentifier = mDatabase.createIdentifier(parentDocumentId); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 206 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 207 | openDevice(parentIdentifier.mDeviceId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 208 | if (parentIdentifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_DEVICE) { |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 209 | final String[] storageDocIds = mDatabase.getStorageDocumentIds(parentDocumentId); |
| 210 | if (storageDocIds.length == 0) { |
| 211 | // Remote device does not provide storages. Maybe it is locked. |
| 212 | return createErrorCursor(projection, R.string.error_locked_device); |
| 213 | } else if (storageDocIds.length > 1) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 214 | // Returns storage list from database. |
| 215 | return mDatabase.queryChildDocuments(projection, parentDocumentId); |
| 216 | } |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 217 | |
| 218 | // Exact one storage is found. Skip storage and returns object in the single |
| 219 | // storage. |
| 220 | parentIdentifier = mDatabase.createIdentifier(storageDocIds[0]); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 221 | } |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 222 | |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 223 | // Returns object list from document loader. |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 224 | return getDocumentLoader(parentIdentifier).queryChildDocuments( |
| 225 | projection, parentIdentifier); |
Daichi Hirono | c18f807 | 2016-02-10 14:59:52 -0800 | [diff] [blame] | 226 | } catch (BusyDeviceException exception) { |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 227 | return createErrorCursor(projection, R.string.error_busy_device); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 228 | } catch (IOException exception) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 229 | Log.e(MtpDocumentsProvider.TAG, "queryChildDocuments", exception); |
Daichi Hirono | 124d060 | 2015-08-11 17:08:35 +0900 | [diff] [blame] | 230 | throw new FileNotFoundException(exception.getMessage()); |
| 231 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 232 | } |
| 233 | |
| 234 | @Override |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 235 | public ParcelFileDescriptor openDocument( |
| 236 | String documentId, String mode, CancellationSignal signal) |
| 237 | throws FileNotFoundException { |
Daichi Hirono | 6213cef | 2016-02-05 17:21:13 +0900 | [diff] [blame] | 238 | if (DEBUG) { |
| 239 | Log.d(TAG, "openDocument: " + documentId); |
| 240 | } |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 241 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 242 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 243 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 244 | final MtpDeviceRecord device = getDeviceToolkit(identifier.mDeviceId).mDeviceRecord; |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 245 | // Turn off MODE_CREATE because openDocument does not allow to create new files. |
| 246 | final int modeFlag = |
| 247 | ParcelFileDescriptor.parseMode(mode) & ~ParcelFileDescriptor.MODE_CREATE; |
| 248 | if ((modeFlag & ParcelFileDescriptor.MODE_READ_ONLY) != 0) { |
| 249 | long fileSize; |
| 250 | try { |
| 251 | fileSize = getFileSize(documentId); |
| 252 | } catch (UnsupportedOperationException exception) { |
| 253 | fileSize = -1; |
| 254 | } |
| 255 | if (MtpDeviceRecord.isPartialReadSupported( |
| 256 | device.operationsSupported, fileSize)) { |
| 257 | return mAppFuse.openFile(Integer.parseInt(documentId), modeFlag); |
| 258 | } else { |
| 259 | // If getPartialObject{|64} are not supported for the device, returns |
| 260 | // non-seekable pipe FD instead. |
| 261 | return getPipeManager(identifier).readDocument(mMtpManager, identifier); |
| 262 | } |
| 263 | } else if ((modeFlag & ParcelFileDescriptor.MODE_WRITE_ONLY) != 0) { |
| 264 | // TODO: Clear the parent document loader task (if exists) and call notify |
| 265 | // when writing is completed. |
| 266 | if (MtpDeviceRecord.isWritingSupported(device.operationsSupported)) { |
| 267 | return mAppFuse.openFile(Integer.parseInt(documentId), modeFlag); |
| 268 | } else { |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 269 | throw new UnsupportedOperationException( |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 270 | "The device does not support writing operation."); |
| 271 | } |
| 272 | } else { |
| 273 | // TODO: Add support for "rw" mode. |
| 274 | throw new UnsupportedOperationException("The provider does not support 'rw' mode."); |
Tomasz Mikolajewski | b80a3cf | 2015-08-24 16:10:51 +0900 | [diff] [blame] | 275 | } |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 276 | } catch (FileNotFoundException | RuntimeException error) { |
| 277 | Log.e(MtpDocumentsProvider.TAG, "openDocument", error); |
| 278 | throw error; |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 279 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 280 | Log.e(MtpDocumentsProvider.TAG, "openDocument", error); |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 281 | throw new IllegalStateException(error); |
Daichi Hirono | 8ba4191 | 2015-07-30 21:22:57 +0900 | [diff] [blame] | 282 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 283 | } |
| 284 | |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 285 | @Override |
| 286 | public AssetFileDescriptor openDocumentThumbnail( |
| 287 | String documentId, |
| 288 | Point sizeHint, |
| 289 | CancellationSignal signal) throws FileNotFoundException { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 290 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 291 | try { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 292 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 293 | return new AssetFileDescriptor( |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 294 | getPipeManager(identifier).readThumbnail(mMtpManager, identifier), |
Daichi Hirono | 573c1fb | 2015-08-11 19:31:30 +0900 | [diff] [blame] | 295 | 0, // Start offset. |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 296 | AssetFileDescriptor.UNKNOWN_LENGTH); |
| 297 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 298 | Log.e(MtpDocumentsProvider.TAG, "openDocumentThumbnail", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 299 | throw new FileNotFoundException(error.getMessage()); |
| 300 | } |
| 301 | } |
| 302 | |
| 303 | @Override |
| 304 | public void deleteDocument(String documentId) throws FileNotFoundException { |
| 305 | try { |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 306 | final Identifier identifier = mDatabase.createIdentifier(documentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 307 | openDevice(identifier.mDeviceId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 308 | final Identifier parentIdentifier = mDatabase.getParentIdentifier(documentId); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 309 | mMtpManager.deleteDocument(identifier.mDeviceId, identifier.mObjectHandle); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 310 | mDatabase.deleteDocument(documentId); |
Daichi Hirono | 76be46f | 2016-04-08 09:48:02 +0900 | [diff] [blame] | 311 | getDocumentLoader(parentIdentifier).cancelTask(parentIdentifier); |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 312 | notifyChildDocumentsChange(parentIdentifier.mDocumentId); |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 313 | if (parentIdentifier.mDocumentType == MtpDatabaseConstants.DOCUMENT_TYPE_STORAGE) { |
| 314 | // If the parent is storage, the object might be appeared as child of device because |
| 315 | // we skip storage when the device has only one storage. |
| 316 | final Identifier deviceIdentifier = mDatabase.getParentIdentifier( |
| 317 | parentIdentifier.mDocumentId); |
| 318 | notifyChildDocumentsChange(deviceIdentifier.mDocumentId); |
| 319 | } |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 320 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 321 | Log.e(MtpDocumentsProvider.TAG, "deleteDocument", error); |
Daichi Hirono | 3faa43a | 2015-08-05 17:15:35 +0900 | [diff] [blame] | 322 | throw new FileNotFoundException(error.getMessage()); |
| 323 | } |
| 324 | } |
| 325 | |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 326 | @Override |
| 327 | public void onTrimMemory(int level) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 328 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 329 | for (final DeviceToolkit toolkit : mDeviceToolkits.values()) { |
| 330 | toolkit.mDocumentLoader.clearCompletedTasks(); |
| 331 | } |
| 332 | } |
Daichi Hirono | 6baa16e | 2015-08-12 13:51:59 +0900 | [diff] [blame] | 333 | } |
| 334 | |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 335 | @Override |
| 336 | public String createDocument(String parentDocumentId, String mimeType, String displayName) |
| 337 | throws FileNotFoundException { |
Daichi Hirono | 6213cef | 2016-02-05 17:21:13 +0900 | [diff] [blame] | 338 | if (DEBUG) { |
| 339 | Log.d(TAG, "createDocument: " + displayName); |
| 340 | } |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 341 | final Identifier parentId; |
| 342 | final MtpDeviceRecord record; |
| 343 | final ParcelFileDescriptor[] pipe; |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 344 | try { |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 345 | parentId = mDatabase.createIdentifier(parentDocumentId); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 346 | openDevice(parentId.mDeviceId); |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 347 | record = getDeviceToolkit(parentId.mDeviceId).mDeviceRecord; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 348 | if (!MtpDeviceRecord.isWritingSupported(record.operationsSupported)) { |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 349 | throw new UnsupportedOperationException( |
| 350 | "Writing operation is not supported by the device."); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 351 | } |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 352 | pipe = ParcelFileDescriptor.createReliablePipe(); |
| 353 | int objectHandle = -1; |
| 354 | MtpObjectInfo info = null; |
| 355 | try { |
| 356 | pipe[0].close(); // 0 bytes for a new document. |
| 357 | |
| 358 | final int formatCode = Document.MIME_TYPE_DIR.equals(mimeType) ? |
| 359 | MtpConstants.FORMAT_ASSOCIATION : |
| 360 | MediaFile.getFormatCode(displayName, mimeType); |
| 361 | info = new MtpObjectInfo.Builder() |
| 362 | .setStorageId(parentId.mStorageId) |
| 363 | .setParent(parentId.mObjectHandle) |
| 364 | .setFormat(formatCode) |
| 365 | .setName(displayName) |
| 366 | .build(); |
| 367 | |
| 368 | final String[] parts = FileUtils.splitFileName(mimeType, displayName); |
| 369 | final String baseName = parts[0]; |
| 370 | final String extension = parts[1]; |
| 371 | for (int i = 0; i <= 32; i++) { |
| 372 | final MtpObjectInfo infoUniqueName; |
| 373 | if (i == 0) { |
| 374 | infoUniqueName = info; |
| 375 | } else { |
Daichi Hirono | 4f04fd3 | 2016-03-18 18:29:56 +0900 | [diff] [blame] | 376 | String suffixedName = baseName + " (" + i + " )"; |
| 377 | if (!extension.isEmpty()) { |
| 378 | suffixedName += "." + extension; |
| 379 | } |
| 380 | infoUniqueName = |
| 381 | new MtpObjectInfo.Builder(info).setName(suffixedName).build(); |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 382 | } |
| 383 | try { |
| 384 | objectHandle = mMtpManager.createDocument( |
| 385 | parentId.mDeviceId, infoUniqueName, pipe[1]); |
| 386 | break; |
| 387 | } catch (SendObjectInfoFailure exp) { |
| 388 | // This can be caused when we have an existing file with the same name. |
| 389 | continue; |
| 390 | } |
| 391 | } |
| 392 | } finally { |
| 393 | pipe[1].close(); |
| 394 | } |
| 395 | if (objectHandle == -1) { |
| 396 | throw new IllegalArgumentException( |
| 397 | "The file name \"" + displayName + "\" is conflicted with existing files " + |
| 398 | "and the provider failed to find unique name."); |
| 399 | } |
Daichi Hirono | 9e8a4fa | 2015-11-19 16:13:38 +0900 | [diff] [blame] | 400 | final MtpObjectInfo infoWithHandle = |
| 401 | new MtpObjectInfo.Builder(info).setObjectHandle(objectHandle).build(); |
| 402 | final String documentId = mDatabase.putNewDocument( |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 403 | parentId.mDeviceId, parentDocumentId, record.operationsSupported, |
Daichi Hirono | 64111e0 | 2016-03-24 21:07:38 +0900 | [diff] [blame] | 404 | infoWithHandle, 0l); |
Daichi Hirono | 76be46f | 2016-04-08 09:48:02 +0900 | [diff] [blame] | 405 | getDocumentLoader(parentId).cancelTask(parentId); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 406 | notifyChildDocumentsChange(parentDocumentId); |
| 407 | return documentId; |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 408 | } catch (FileNotFoundException | RuntimeException error) { |
| 409 | Log.e(TAG, "createDocument", error); |
| 410 | throw error; |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 411 | } catch (IOException error) { |
Daichi Hirono | 6a5ea7e | 2016-02-02 16:35:03 +0900 | [diff] [blame] | 412 | Log.e(TAG, "createDocument", error); |
Daichi Hirono | fc7fb75 | 2016-03-15 19:19:31 +0900 | [diff] [blame] | 413 | throw new IllegalStateException(error); |
Tomasz Mikolajewski | 87763e6 | 2015-08-10 10:10:22 +0900 | [diff] [blame] | 414 | } |
| 415 | } |
| 416 | |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 417 | void openDevice(int deviceId) throws IOException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 418 | synchronized (mDeviceListLock) { |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 419 | if (mDeviceToolkits.containsKey(deviceId)) { |
| 420 | return; |
| 421 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 422 | if (DEBUG) { |
| 423 | Log.d(TAG, "Open device " + deviceId); |
| 424 | } |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 425 | final MtpDeviceRecord device = mMtpManager.openDevice(deviceId); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 426 | final DeviceToolkit toolkit = |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 427 | new DeviceToolkit(mMtpManager, mResolver, mDatabase, device); |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 428 | mDeviceToolkits.put(deviceId, toolkit); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 429 | mIntentSender.sendUpdateNotificationIntent(); |
| 430 | try { |
| 431 | mRootScanner.resume().await(); |
| 432 | } catch (InterruptedException error) { |
| 433 | Log.e(TAG, "openDevice", error); |
| 434 | } |
Daichi Hirono | 4e94b8d | 2016-02-21 22:42:41 +0900 | [diff] [blame] | 435 | // Resume document loader to remap disconnected document ID. Must be invoked after the |
| 436 | // root scanner resumes. |
| 437 | toolkit.mDocumentLoader.resume(); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 438 | } |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 439 | } |
| 440 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 441 | void closeDevice(int deviceId) throws IOException, InterruptedException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 442 | synchronized (mDeviceListLock) { |
| 443 | closeDeviceInternal(deviceId); |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 444 | } |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 445 | mRootScanner.resume(); |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 446 | mIntentSender.sendUpdateNotificationIntent(); |
Daichi Hirono | d515242 | 2015-07-15 13:31:51 +0900 | [diff] [blame] | 447 | } |
| 448 | |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 449 | MtpDeviceRecord[] getOpenedDeviceRecordsCache() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 450 | synchronized (mDeviceListLock) { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 451 | final MtpDeviceRecord[] records = new MtpDeviceRecord[mDeviceToolkits.size()]; |
| 452 | int i = 0; |
| 453 | for (final DeviceToolkit toolkit : mDeviceToolkits.values()) { |
| 454 | records[i] = toolkit.mDeviceRecord; |
| 455 | i++; |
Daichi Hirono | 20754c5 | 2015-12-15 18:52:26 +0900 | [diff] [blame] | 456 | } |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 457 | return records; |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 458 | } |
Daichi Hirono | 2efe4ca | 2015-07-27 16:47:46 +0900 | [diff] [blame] | 459 | } |
Daichi Hirono | 50d17aa | 2015-07-28 15:49:01 +0900 | [diff] [blame] | 460 | |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 461 | /** |
Daichi Hirono | 1e37444 | 2016-02-11 10:08:21 -0800 | [diff] [blame] | 462 | * Obtains document ID for the given device ID. |
| 463 | * @param deviceId |
| 464 | * @return document ID |
| 465 | * @throws FileNotFoundException device ID has not been build. |
| 466 | */ |
| 467 | public String getDeviceDocumentId(int deviceId) throws FileNotFoundException { |
| 468 | return mDatabase.getDeviceDocumentId(deviceId); |
| 469 | } |
| 470 | |
| 471 | /** |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 472 | * Resumes root scanner to handle the update of device list. |
| 473 | */ |
| 474 | void resumeRootScanner() { |
Daichi Hirono | ebd2405 | 2016-02-06 21:05:57 +0900 | [diff] [blame] | 475 | if (DEBUG) { |
| 476 | Log.d(MtpDocumentsProvider.TAG, "resumeRootScanner"); |
| 477 | } |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 478 | mRootScanner.resume(); |
| 479 | } |
| 480 | |
| 481 | /** |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 482 | * Finalize the content provider for unit tests. |
| 483 | */ |
| 484 | @Override |
| 485 | public void shutdown() { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 486 | synchronized (mDeviceListLock) { |
| 487 | try { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 488 | // Copy the opened key set because it will be modified when closing devices. |
| 489 | final Integer[] keySet = |
| 490 | mDeviceToolkits.keySet().toArray(new Integer[mDeviceToolkits.size()]); |
| 491 | for (final int id : keySet) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 492 | closeDeviceInternal(id); |
| 493 | } |
Daichi Hirono | 2e9a57b | 2016-02-26 17:41:45 +0900 | [diff] [blame] | 494 | mRootScanner.pause(); |
Daichi Hirono | acb0e27 | 2016-03-14 21:49:14 +0900 | [diff] [blame] | 495 | } catch (InterruptedException | IOException | TimeoutException e) { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 496 | // It should fail unit tests by throwing runtime exception. |
| 497 | throw new RuntimeException(e); |
| 498 | } finally { |
| 499 | mDatabase.close(); |
Daichi Hirono | b36b155 | 2016-01-25 14:26:14 +0900 | [diff] [blame] | 500 | mAppFuse.close(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 501 | super.shutdown(); |
| 502 | } |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 503 | } |
| 504 | } |
| 505 | |
Daichi Hirono | 5fecc6c | 2015-08-04 17:45:51 +0900 | [diff] [blame] | 506 | private void notifyChildDocumentsChange(String parentDocumentId) { |
| 507 | mResolver.notifyChange( |
| 508 | DocumentsContract.buildChildDocumentsUri(AUTHORITY, parentDocumentId), |
| 509 | null, |
| 510 | false); |
| 511 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 512 | |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 513 | /** |
Daichi Hirono | be38848 | 2015-12-14 16:20:14 +0900 | [diff] [blame] | 514 | * Clears MTP identifier in the database. |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 515 | */ |
| 516 | private void resume() { |
| 517 | synchronized (mDeviceListLock) { |
| 518 | mDatabase.getMapper().clearMapping(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 519 | } |
| 520 | } |
| 521 | |
| 522 | private void closeDeviceInternal(int deviceId) throws IOException, InterruptedException { |
| 523 | // TODO: Flush the device before closing (if not closed externally). |
Daichi Hirono | fda7474 | 2016-02-01 13:00:31 +0900 | [diff] [blame] | 524 | if (!mDeviceToolkits.containsKey(deviceId)) { |
| 525 | return; |
| 526 | } |
Daichi Hirono | 19aa932 | 2016-02-04 14:19:52 +0900 | [diff] [blame] | 527 | if (DEBUG) { |
| 528 | Log.d(TAG, "Close device " + deviceId); |
| 529 | } |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 530 | getDeviceToolkit(deviceId).close(); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 531 | mDeviceToolkits.remove(deviceId); |
| 532 | mMtpManager.closeDevice(deviceId); |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 533 | } |
| 534 | |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 535 | private DeviceToolkit getDeviceToolkit(int deviceId) throws FileNotFoundException { |
Daichi Hirono | e0282dd | 2015-11-26 15:20:08 +0900 | [diff] [blame] | 536 | synchronized (mDeviceListLock) { |
Daichi Hirono | e1d5771 | 2015-11-17 10:55:45 +0900 | [diff] [blame] | 537 | final DeviceToolkit toolkit = mDeviceToolkits.get(deviceId); |
| 538 | if (toolkit == null) { |
| 539 | throw new FileNotFoundException(); |
| 540 | } |
| 541 | return toolkit; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 542 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 543 | } |
| 544 | |
| 545 | private PipeManager getPipeManager(Identifier identifier) throws FileNotFoundException { |
| 546 | return getDeviceToolkit(identifier.mDeviceId).mPipeManager; |
| 547 | } |
| 548 | |
| 549 | private DocumentLoader getDocumentLoader(Identifier identifier) throws FileNotFoundException { |
| 550 | return getDeviceToolkit(identifier.mDeviceId).mDocumentLoader; |
| 551 | } |
| 552 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 553 | private long getFileSize(String documentId) throws FileNotFoundException { |
| 554 | final Cursor cursor = mDatabase.queryDocument( |
| 555 | documentId, |
| 556 | MtpDatabase.strings(Document.COLUMN_SIZE, Document.COLUMN_DISPLAY_NAME)); |
| 557 | try { |
| 558 | if (cursor.moveToNext()) { |
Daichi Hirono | 77a1c656 | 2016-03-28 14:37:12 +0900 | [diff] [blame] | 559 | if (cursor.isNull(0)) { |
| 560 | throw new UnsupportedOperationException(); |
| 561 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 562 | return cursor.getLong(0); |
| 563 | } else { |
| 564 | throw new FileNotFoundException(); |
| 565 | } |
| 566 | } finally { |
| 567 | cursor.close(); |
| 568 | } |
| 569 | } |
| 570 | |
Daichi Hirono | 2965776 | 2016-02-10 16:55:37 -0800 | [diff] [blame] | 571 | /** |
| 572 | * Creates empty cursor with specific error message. |
| 573 | * |
| 574 | * @param projection Column names. |
| 575 | * @param stringResId String resource ID of error message. |
| 576 | * @return Empty cursor with error message. |
| 577 | */ |
| 578 | private Cursor createErrorCursor(String[] projection, int stringResId) { |
| 579 | final Bundle bundle = new Bundle(); |
| 580 | bundle.putString(DocumentsContract.EXTRA_ERROR, mResources.getString(stringResId)); |
| 581 | final Cursor cursor = new MatrixCursor(projection); |
| 582 | cursor.setExtras(bundle); |
| 583 | return cursor; |
| 584 | } |
| 585 | |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 586 | private static class DeviceToolkit implements AutoCloseable { |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 587 | public final PipeManager mPipeManager; |
| 588 | public final DocumentLoader mDocumentLoader; |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 589 | public final MtpDeviceRecord mDeviceRecord; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 590 | |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 591 | public DeviceToolkit(MtpManager manager, |
| 592 | ContentResolver resolver, |
| 593 | MtpDatabase database, |
| 594 | MtpDeviceRecord record) { |
Daichi Hirono | f578fa2 | 2016-02-19 18:05:42 +0900 | [diff] [blame] | 595 | mPipeManager = new PipeManager(database); |
Daichi Hirono | 61ba923 | 2016-02-26 12:58:39 +0900 | [diff] [blame] | 596 | mDocumentLoader = new DocumentLoader(record, manager, resolver, database); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 597 | mDeviceRecord = record; |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 598 | } |
Daichi Hirono | 24ab92a | 2016-03-04 17:53:03 +0900 | [diff] [blame] | 599 | |
| 600 | @Override |
| 601 | public void close() throws InterruptedException { |
| 602 | mPipeManager.close(); |
| 603 | mDocumentLoader.close(); |
| 604 | } |
Tomasz Mikolajewski | 4c1d3dd | 2015-09-02 13:27:46 +0900 | [diff] [blame] | 605 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 606 | |
| 607 | private class AppFuseCallback implements AppFuse.Callback { |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 608 | private final Map<Long, MtpFileWriter> mWriters = new HashMap<>(); |
| 609 | |
| 610 | @Override |
| 611 | public long getFileSize(int inode) throws FileNotFoundException { |
| 612 | return MtpDocumentsProvider.this.getFileSize(String.valueOf(inode)); |
| 613 | } |
| 614 | |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 615 | @Override |
Daichi Hirono | 2f310f6 | 2016-01-27 12:34:29 +0900 | [diff] [blame] | 616 | public long readObjectBytes( |
| 617 | int inode, long offset, long size, byte[] buffer) throws IOException { |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 618 | final Identifier identifier = mDatabase.createIdentifier(Integer.toString(inode)); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 619 | final MtpDeviceRecord record = getDeviceToolkit(identifier.mDeviceId).mDeviceRecord; |
Daichi Hirono | 77a1c656 | 2016-03-28 14:37:12 +0900 | [diff] [blame] | 620 | |
| 621 | if (MtpDeviceRecord.isSupported( |
| 622 | record.operationsSupported, MtpConstants.OPERATION_GET_PARTIAL_OBJECT_64)) { |
| 623 | return mMtpManager.getPartialObject64( |
| 624 | identifier.mDeviceId, identifier.mObjectHandle, offset, size, buffer); |
| 625 | } |
| 626 | |
| 627 | if (0 <= offset && offset <= 0xffffffffL && MtpDeviceRecord.isSupported( |
| 628 | record.operationsSupported, MtpConstants.OPERATION_GET_PARTIAL_OBJECT)) { |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 629 | return mMtpManager.getPartialObject( |
| 630 | identifier.mDeviceId, identifier.mObjectHandle, offset, size, buffer); |
Daichi Hirono | 0f32537 | 2016-02-21 15:50:30 +0900 | [diff] [blame] | 631 | } |
Daichi Hirono | 77a1c656 | 2016-03-28 14:37:12 +0900 | [diff] [blame] | 632 | |
| 633 | throw new UnsupportedOperationException(); |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 634 | } |
| 635 | |
| 636 | @Override |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 637 | public int writeObjectBytes( |
| 638 | long fileHandle, int inode, long offset, int size, byte[] bytes) |
| 639 | throws IOException, ErrnoException { |
| 640 | final MtpFileWriter writer; |
| 641 | if (mWriters.containsKey(fileHandle)) { |
| 642 | writer = mWriters.get(fileHandle); |
| 643 | } else { |
| 644 | writer = new MtpFileWriter(mContext, String.valueOf(inode)); |
| 645 | mWriters.put(fileHandle, writer); |
| 646 | } |
| 647 | return writer.write(offset, size, bytes); |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 648 | } |
Daichi Hirono | 09ece6c | 2016-01-20 19:09:25 +0900 | [diff] [blame] | 649 | |
| 650 | @Override |
Daichi Hirono | f4e7fa8 | 2016-03-28 16:07:45 +0900 | [diff] [blame] | 651 | public void flushFileHandle(long fileHandle) throws IOException, ErrnoException { |
| 652 | final MtpFileWriter writer = mWriters.get(fileHandle); |
| 653 | if (writer == null) { |
| 654 | // File handle for reading. |
| 655 | return; |
| 656 | } |
| 657 | final MtpDeviceRecord device = getDeviceToolkit( |
| 658 | mDatabase.createIdentifier(writer.getDocumentId()).mDeviceId).mDeviceRecord; |
| 659 | writer.flush(mMtpManager, mDatabase, device.operationsSupported); |
| 660 | } |
| 661 | |
| 662 | @Override |
| 663 | public void closeFileHandle(long fileHandle) throws IOException, ErrnoException { |
| 664 | final MtpFileWriter writer = mWriters.get(fileHandle); |
| 665 | if (writer == null) { |
| 666 | // File handle for reading. |
| 667 | return; |
| 668 | } |
| 669 | try { |
| 670 | writer.close(); |
| 671 | } finally { |
| 672 | mWriters.remove(fileHandle); |
| 673 | } |
Daichi Hirono | 09ece6c | 2016-01-20 19:09:25 +0900 | [diff] [blame] | 674 | } |
Daichi Hirono | f52ef00 | 2016-01-11 18:07:01 +0900 | [diff] [blame] | 675 | } |
Daichi Hirono | c00d5d4 | 2015-05-28 11:17:41 -0700 | [diff] [blame] | 676 | } |